用 vba 抓取网页,从 bgcolor 中提取数字 e 代码颜色

scraping web with vba, extract number e code color from bgcolor

你好我想统计一下td标签下有多少个bgcolor,然后从属性"bgcolor"

中提取颜色代码
<td bgcolor="#1ea8ec" style="color:#ffffff">2.</td>
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 则它采用代码颜色并为单元格着色

有更好的方法。

  1. 使用 xmlhttp 避免打开浏览器
  2. 使用 css 选择器进行更快的检索
  3. 为 table 使用 id 选择器,而不是 class 名称,因为这是最佳选择方法
  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

结果: