【问题标题】:scraping web with vba, extract number e code color from bgcolor用vba抓取网页,从bgcolor中提取数字e代码颜色
【发布时间】:2025-12-24 00:55:12
【问题描述】:

你好,我想计算一下标签td下有多少个bgcolor,并从属性“bgcolor”中提取颜色代码

<td bgcolor="#1ea8ec" style="color:#ffffff">2.</td>

【问题讨论】:

  • 是什么确切地阻止您这样做?请更新您现有的代码并描述您遇到的问题。

标签: excel vba web-scraping nested-attributes


【解决方案1】:

有更好的方法。

  1. 使用xmlhttp避免打开浏览器
  2. 使用 css 选择器加快检索速度
  3. 对表使用 id 选择器,而不是类名,因为这是最佳选择方法
  4. 通过使用单个循环生成 nodeList 来减少循环
  5. 使用字典确保只存储唯一值

VBA:

Option Explicit
Public Sub GetColourCodes()
    Dim html As HTMLDocument, dict As Object, i As Long, colourCodes As Object
    Set html = New HTMLDocument: Set dict = CreateObject("Scripting.Dictionary")
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", "http://www.soccer-rating.com/France/", False
        .send
        html.body.innerHTML = StrConv(.responseBody, vbUnicode)
    End With
    Set colourCodes = html.querySelectorAll("#ltable td[bgcolor]")
    For i = 0 To colourCodes.Length - 1
        dict(colourCodes.item(i).bgColor) = vbNullString
    Next
    Stop '<=Delete me later
End Sub

结果:

【讨论】:

    【解决方案2】:
    Sub rating()
    Set objIE = CreateObject("internetexplorer.application")
    
    Dim itemEle As Object
    objIE.Visible = True
    objIE.navigate "http://www.soccer-rating.com/France/"
    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
    
    Set itemEle = objIE.document.getElementsByclassname("ltable")(0)
    i = 0
    For Each tr In itemEle.getElementsBytagname("tr")
        color = 
        j = 2
        For Each td In tr.getElementsBytagname("td")
        'if bgcolor there is then
              Sheets("rating").Cells(i, j).interior.color  = 'here color code of bgcolor "#1ea8ec"
         'end if
         j = j + 1
        Next td
        i = i + 1
    
    Next tr
    End Sub
    

    如果它找到 bgcolor 然后它会采用代码颜色并为单元格着色

    【讨论】:

    • 我用命令x = td.getAttribute ("bgcolor")解决了