[英]Excel VBA Internet explorer - information from 2 columns within a table
我正在尝试从网页获取信息。 我需要的信息在表格中,我无法直接解决。 这是页面: 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
试试下面的代码:
仅供参考:在表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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.