从 A HTML Table 抓取数据

Scrape Data From A HTML Table

我真的很难尝试提取网络的一些数据 table。我过去曾抓取过网络数据,但从未从 table 抓取过,无法解决

我尝试了几种变体,但似乎没有任何效果,我已经多次更改 class 和子节点编号以反映每个项目,但是我无法从 table 中提取任何内容

Q) 有人可以建议 table class 以及如何从 td

中提取

我在这个论坛和其他论坛上阅读了一些关于从 table 抓取的文章 post,但是 none 帮助了我,因此 post

  ''''Data 1
        On Error Resume Next
        If doc.getElementsByClassName("content")(0).getElementsByTagName("td").Children(0) Is Nothing Then
            wsSheet.Cells(StartRow + myCounter, 1).Value = "-"
        Else
         On Error Resume Next
            wsSheet.Cells(StartRow + myCounter, 1).Value = doc.getElementsByClassName("content")(0).getElementsByTagName("td").Children(0).innerText
        End If

我尝试了以下变体

doc.getElementsByClassName("content")(0)
doc.getElementsByClassName("content")(0)).Children(0)
doc.getElementsByClassName("content")(0).getElementsByTagName("th").getElementsByTagName("td").Children(0)
doc.getElementsByClassName("content")(0).getElementsByTagName("td").Children(0)

这是 html 的图像,我尝试输入 html 代码,但无法使其看起来正确

一如既往地提前致谢

首先是一个建议:将这些语句拆分成多个部分并将结果保存到中间变量中。

然后观察:<td>标签没有children,所以children(0)会returnNothing<th>在该页面上有一个 child,<span>-tag) 。您可能想要读取单元格的内容,您可以使用 属性 InnerHtml.

删除 On Error Resume Next 语句。只要你在开发你的例程,让代码 运行 进入错误,这样你就可以轻松调试并看到代码失败的地方。一旦你准备好了,最好自己检查错误。

不确定以下是否合适,但它应该给你一个想法:

' Fetch the "Content"-DIV
Dim content As Object
Set content = HtmlDoc.getElementsByClassName("content")(0)

' Fetch the first table with that div
Dim table As Object
Set table = content.getElementsByTagName("table")(0)

' Loop over all <td>-Tags and print the content
Dim td As Object
For Each td In table.getElementsByTagName("td")
    Debug.Print td.innerHTML
    If td.Children.Length > 0 Then
        ' If <td> has children, fetch the first child and show the content
        Dim child As Object
        Set child = td.Children(0)
        Debug.Print " We found a child: " & child.tagName, child.innerHTML
    End If
Next

调试代码时,记得使用VBA的“LocalsWindow”(View->LocalsWindow)。在那里您可以检查 objects.

的所有详细信息