【问题标题】:Scrape Data From A HTML Table [duplicate]从 HTML 表中抓取数据 [重复]
【发布时间】:2021-02-10 14:25:05
【问题描述】:

我在尝试提取网络表格的一些数据时真的很吃力。我过去曾抓取过网络数据,但从未从表格中抓取过,也无法计算出来

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

Q) 有人可以就表格类以及如何从td 中提取信息提出建议

我在这个论坛和其他论坛上阅读了几篇关于从桌子上抓取的帖子,但是没有任何帮助,因此这篇帖子

  ''''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 代码,但无法正确显示

一如既往地提前感谢

【问题讨论】:

  • ie.document.querySelector(".contact-table").outerHTML 复制到剪贴板或循环 ie.document.querySelector(".contact-table") 的行和列

标签: excel vba web-scraping screen-scraping


【解决方案1】:

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

然后观察:<td>-tag 没有子标签,因此 children(0) 将返回 Nothing(该页面上的 <th> 有一个子标签,<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的“Locals Window”(View->Locals Window)。在那里您可以检查对象的所有细节。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-09
    • 2013-02-22
    • 1970-01-01
    • 1970-01-01
    • 2010-09-07
    • 2017-12-22
    相关资源
    最近更新 更多