在没有任何 Names/IDs 的情况下使用 VBA 从网站上的 table 中提取价值

Pulling value from table on website using VBA without any Names/IDs

我正在使用 VBA 尝试从县评估员的网站检索数据。我在其他部分使用了getElementByName/ID等,但似乎没有标识符,数据只是在一个普通的网格中。有没有办法用不同的方法检索这些数据?这个网页还有其他几个格子,感觉好难找

这是 HTML 源代码:

<div class="grid">
<table class="colborder w95">
<caption>Current Values</caption>
<thead><tr class="allborder"><th class="toleft">Type</th><th class="toleft">Class</th><th class="toright">Kind</th><th class="number">Land</th><th class="number">Bldg</th>
<th class="number">Total</th></tr>
</thead>
<tbody>
<tr class=" ">
<td class="tobottom toleft " >2020&#160;Value</td>
<td class="tobottom toleft rowborder" >Residential</td>
<td class="toright rowborder">Full</td>
<td class="number rowborder">,600</td>
<td class="number rowborder">5,300</td>
<td class="number rowborder">8,900</td></tr>

我要提取的数字是 table 的右下角,在本例中为 298,900 美元。我唯一能想到的区分这一部分的是“当前值”,这是网格的标题。我将如何使用此标题定位 table 并从右下角提取值?

如有任何帮助,我们将不胜感激!

乔尔

像这样的东西有点笨拙,但对我有用:

Dim doc As New HTMLDocument, tbls As Object, tbl As Object

doc.body.innerHTML = Range("A2").Value 'your sample HTML with closing </table></div>...

Set tbls = doc.getElementsByTagName("table")
For Each tbl In tbls
    If tbl.Caption.innerHTML = "Current Values" Then
        Debug.Print tbl.Rows(1).Cells(5).innerText
        Exit For
    End If
Next tbl

以下是从 HTML table.

中获取特定单元格数据的替代方法

如果元素不包含 ID、名称或任何其他属性,您可以尝试通过 TagName 访问该元素。

然后您可以使用其索引键值引用特定元素。注意索引键从0开始。

示例代码:

Sub demo()
 Dim IE
 Dim ro As Integer
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    IE.navigate "https://Your_site_address_will_be_here"

    Do While IE.Busy
        Application.Wait DateAdd("s", 1, Now)
    Loop
          
    Set Table = IE.document.getElementsByTagName("table")
    Set tRows = Table(0).getElementsByTagName("tr")
               
     ro = 1
     For Each r In tRows
          Set tcells = r.getElementsByTagName("td")
            For Each c In tcells
                If ro = 6 Then
                Debug.Print (c.innerText)
                ro = 0
                End If
                ro = ro + 1
            Next
     Next
         
End Sub

输出:

另外,您可以根据自己的需要修改代码示例。