简体   繁体   中英

Excel VBA Internet explorer - information from 2 columns within a table

I am trying to get information from a webpage. The information i need is in a table and I am failing to address it directly. this is the page: http://www.idealo.de/preisvergleich/MainSearchProductCategory.html?q=matratze

the table id is: plist each row i need has the name "js4offer" and than i need the 2nd and the 3rd "tr" from within the table-row but i have no idea how i can loop trough all the elements within this one table. i tried out thousands of code snippet but none of them is doing what i wanna do ;)

here´s my code so far:

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

Try below code :

FYI : In Table plist except for the first all the tr have name "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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM