【发布时间】:2017-05-21 18:42:15
【问题描述】:
我尝试使用 selenium 从网页中抓取表格数据。但是,它会解析该页面中的所有表,但我需要一个。我找不到关于如何选择单个表格的任何想法。这是我尝试过的:
Sub table_data()
Dim driver As New WebDriver
Dim tabl As Object, rdata As Object, cdata As Object
Set driver = New WebDriver
driver.Start "Phantomjs", "https://fantasy.premierleague.com"
driver.get "/player-list/"
For Each tabl In driver.FindElementsByXPath("//table[@class='ism-table']")
For Each rdata In tabl.FindElementsByXPath(".//tr")
For Each cdata In rdata.FindElementsByXPath(".//td")
y = y + 1
Cells(x, y) = cdata.Text
Next cdata
x = x + 1
y = 0
Next rdata
Next tabl
End Sub
不过,我是用 XHR 做到的!
Sub TableData()
Dim xmlpage As New XMLHTTP60
Dim htmldoc As New MSHTML.HTMLDocument
Dim htmlas As Object, tRow As Object, tCel As Object
x = 1
With xmlpage
.Open "GET", "https://fantasy.premierleague.com/player-list/", False
.send
htmldoc.body.innerHTML = .responseText
End With
Set htmlas = htmldoc.getElementsByTagName("table")(2)
For Each tRow In htmlas.Rows
For Each tCel In tRow.Cells
c = c + 1
Cells(x, c) = tCel.innerText
Next tCel
c = 0
x = x + 1
Next tRow
End Sub
【问题讨论】:
-
你需要哪张桌子?你不能用索引得到它吗?
-
感谢 PRAISER 的回答。外面有八张桌子。任何人都会做。在索引的情况下,我不明白我应该把那个数字放在我的代码中的什么地方?
标签: vba selenium web-scraping