【发布时间】:2025-12-24 00:55:12
【问题描述】:
你好,我想计算一下标签td下有多少个bgcolor,并从属性“bgcolor”中提取颜色代码
<td bgcolor="#1ea8ec" style="color:#ffffff">2.</td>
【问题讨论】:
-
是什么确切地阻止您这样做?请更新您现有的代码并描述您遇到的问题。
标签: excel vba web-scraping nested-attributes
你好,我想计算一下标签td下有多少个bgcolor,并从属性“bgcolor”中提取颜色代码
<td bgcolor="#1ea8ec" style="color:#ffffff">2.</td>
【问题讨论】:
标签: excel vba web-scraping nested-attributes
有更好的方法。
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
结果:
【讨论】:
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 然后它会采用代码颜色并为单元格着色
【讨论】: