Excel VBA - 获取网站上的所有 href 链接

标签 excel vba web-scraping export-to-excel

Example

您好,希望有人能帮助我。 在此示例链接中: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 属性。


所需引用资料:

  1. 微软互联网控制
  2. 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

阅读:

  1. :nth-child()
  2. Child combinator

关于Excel VBA - 获取网站上的所有 href 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68328816/

相关文章:

excel - 无法获取 Worksheet 类的 Copy 属性

java - Apache POI : How do I set the dataFormat of a cell based on a Java DateTimeFormatter

excel - 使用 vlookup 公式的列引用

excel - 在 VBA 中使用索引匹配的范围与范围单元格

excel - 如何在文本输入期间自动调整 Excel 中的列宽

excel - VBA循环遍历具有嵌套for循环的列

excel - 重新激活工作簿时执行操作

javascript - 在自动脚本中从网站检索 zip 文件

javascript - 抓取动态数据

Python 网络抓取线程性能