您好,希望有人能帮助我。 在此示例链接中:https://www.academiadasapostas.com/stats/competition/brasil/26
我想获取所有作为“VS”目标的href链接。 我正在尝试这样的例子:
Sub ScrapeScores()
Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLTables As MSHTML.IHTMLElementCollection
Dim HTMLTable As MSHTML.IHTMLElement
Dim HTMLDiv As MSHTML.IHTMLElement
Dim TableSection As MSHTML.IHTMLElement
Dim TableRow As MSHTML.IHTMLElement
Dim TableCell As MSHTML.IHTMLElement
Dim RowText As String
IE.Visible = True
IE.navigate "https://www.academiadasapostas.com/stats/competition/brasil/26"
Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy
Loop
Set HTMLDoc = IE.document
Set HTMLDiv = HTMLDoc.getElementById("competition-round-group-0")
Set HTMLTables = HTMLDiv.getElementsByTagName("a")
For Each HTMLTable In HTMLTables
Debug.Print HTMLTable.ID, "&", HTMLTable.className
For Each TableSection In HTMLTable.Children
Debug.Print , TableSection.tagName
Next TableSection
Next HTMLTable
End Sub
但没有成功。我想我可以将 CSS 与 SelectorAll 一起使用,对吧?由于 IE 即将消失,最好使用 CSS 来代替。
提前感谢您的回答。
最佳答案
您可以使用以下 css 模式 querySelectorAll .competition-rounds td:nth-child(4) > a
。循环返回的nodeList
并从每个节点中提取href
。这将选择该表中的第 4 列,然后是子 a
标记,在循环期间从中提取 href
属性。
所需引用资料:
- 微软互联网控制
- Microsoft HTML 对象库
Option Explicit
Public Sub PrintLinks()
Dim ie As SHDocVw.InternetExplorer, nodeList As MSHTML.IHTMLDOMChildrenCollection
Set ie = New SHDocVw.InternetExplorer
With ie
.Visible = True
.Navigate2 "https://www.academiadasapostas.com/stats/competition/brasil/26"
While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend
Set nodeList = ie.Document.querySelectorAll(".competition-rounds td:nth-child(4) > a")
Dim i As Long
For i = 0 To nodeList.length - 1
Debug.Print nodeList.Item(i).href
Next
Stop
.Quit
End With
End Sub
阅读:
关于Excel VBA - 获取网站上的所有 href 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68328816/