1

我正在尝试从网页获取信息。我需要的信息在一个表格中,我没有直接解决它。这是页面: http ://www.idealo.de/preisvergleich/MainSearchProductCategory.html?q=matratze

表ID是:plist我需要的每一行都有名称“js4offer”,而不是我需要表行中的第二个和第三个“tr”,但我不知道如何循环遍历这一行中的所有元素桌子。我尝试了数千个代码片段,但没有一个在做我想做的事;)

到目前为止,这是我的代码:

Sub website()

    Set sht = Sheets("Tabelle4")
    rCount = 1

    Set objIE = CreateObject("InternetExplorer.application")

    With objIE
        .Visible = True
        .Navigate "http://www.idealo.de/preisvergleich/MainSearchProductCategory.html?q=Matratze"

        Do While .Busy Or .ReadyState <> 4
            DoEvents
        Loop

        Set objHTML = objIE.Document
        readText = objHTML.body.outerHTML
        Cells(1, 1) = readText

        Set myElements = objHTML.getElementById("plist")

        For Each ele In myElements

            If ele.getElementsByName("js4offer") Then
                rCount = rCount + 1
                Cells("A", rCount) = ele.Item(1)
                Cells("B", rCount) = ele.Item(2)
            End If

        Next ele

    End With

    objIE.Quit
    Set objIE = Nothing

End Sub
4

1 回答 1

1

试试下面的代码:

仅供参考:在表 plist 中,除了第一个之外,所有 tr 的名称都为“js4offer”。

Sub website()


' Set sht = Sheets("Tabelle4")
'  rCount = 1

    Dim objIE As Object, objTbl As Object, objTR As Object
    Set objIE = CreateObject("InternetExplorer.application")

    With objIE
        .Visible = True
        .Navigate "http://www.idealo.de/preisvergleich/MainSearchProductCategory.html?q=Matratze"

        Do While .Busy Or .ReadyState <> 4
            DoEvents
        Loop

        Set objTbl = objIE.Document.getElementById("plist")
        Set objTR = objTbl.getElementsbyTagName("tr")


        rCount = 1
        On Error Resume Next
        For Each td In objTR
            Cells(rCount, 1) = td.all(0).innerText
            Cells(rCount, 2) = td.all(1).innerText
            Cells(rCount, 3) = td.all(2).innerText
           ' Cells(rCount, 4) = td.all(3).innerText
            'Cells(rCount, 5) = td.all(4).innerText
            'Cells(rCount, 6) = td.all(5).innerText
            'Cells(rCount, 7) = td.all(6).innerText
            rCount = rCount + 1
        Next
        On Error GoTo 0

    End With

    objIE.Quit
    Set objIE = Nothing

End Sub
于 2013-03-18T17:11:29.570 回答