Html 如何使用Excel宏刮取嵌套在Div id中的表类的第一列

Html 如何使用Excel宏刮取嵌套在Div id中的表类的第一列,html,excel,vba,web-scraping,Html,Excel,Vba,Web Scraping,好的,这是目标网页: 以下是我当前的代码: Sub GetItemsList() ' This macro uses manually entered links to scrap the content of the target page. ' It does not (yet) capture hyperlinks, it only grabs text. Dim ie As Object Dim retStr As String Dim sht As Worksheet Dim Las

好的,这是目标网页:

以下是我当前的代码:

Sub GetItemsList()
' This macro uses manually entered links to scrap the content of the target page.
' It does not (yet) capture hyperlinks, it only grabs text.
Dim ie As Object
Dim retStr As String
Dim sht As Worksheet
Dim LastRow As Long
Dim rCell As Range
Dim rRng As Range
Dim Count As Long
Dim Status As String
Dim BadCount As Long


Set sht = ThisWorkbook.Worksheets("List")
BadCount = 0

LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
    Set ie = CreateObject("internetexplorer.application")
    Set rRng = sht.Range("b1:b" & LastRow)
    Status = "Starting at row "
    For Each rCell In rRng.Cells
        Count = rCell.Row
        Application.StatusBar = BadCount & " dead links so far. " & Status & Count & "of " & LastRow & "."
        Wait 1
        If rCell = "" Then
            With ie
                .Navigate rCell.Offset(0, -1).Value
                .Visible = False
            End With
            Do While ie.Busy
                DoEvents
            Loop
            Wait 1

            On Error GoTo ErrHandler
'            rCell.Value = ie.Document.getElementById("content").innerText
            rCell.Value = ie.Document.getElementsByClassName("common").innerText
            rCell.WrapText = False
            Status = "This row successfully scraped. Moving on to row "
            Application.StatusBar = BadCount & " dead links so far. " & Status & Count + 1 & "of " & LastRow & "."
            Status = "Previous row succeded. Now at row "
98            Wait 1
        End If
    Next rCell
    If BadCount > 0 Then
        Application.StatusBar = "Macro finshed running with " & BadCount & " errors."
        Else
        Application.StatusBar = "Finished."
    End If
    Exit Sub
ErrHandler:
    rCell.Value = ""
    Status = "Previous row failed. Moving on to row "
    BadCount = BadCount + 1
    Application.StatusBar = "This row is a dead link. " & BadCount & " dead links so far. Moving on to row " & Count + 1 & "of " & LastRow & "."
    Resume 98
End Sub
(试着忽略我所有的状态栏更新,这段代码最初是为了一个冗长的超链接列表,我需要(当时)知道什么时候出了问题)

现在,注释掉的行起作用了,因为它从
divid
内容中获取整个文本体。但是我想获取嵌套在表的第一列中的超链接,该列嵌套在
div id
中(这是下面一行的目的)。但它只是失败了。Excel不执行任何操作,将其视为错误,然后继续下一个链接

我假设我需要告诉Excel在
Div id
中查找
表类。但我不知道该怎么做,我还没弄明白


谢谢大家。

我会使用CSS选择器来定位链接,作为一种比启动浏览器更快的检索方法


CSS选择器:

以下是:

td:first-child [href]
td:第一个子元素是
td
标记元素的一部分<代码>“
是一个,而
[]
是一个。基本上,在本例中,它为每行的第一个
td
元素(即第一列)进行选择,然后选择其中的
href
属性元素

:first-child CSS伪类表示 一组同级元素

遗憾的是,VBA实现不支持选择器,因为确切的元素也可以与
匹配。common tr+tr td:not([href*='rule'],br)
。对伪选择器的支持非常有限。在这种情况下,如果子体组合中支持使用CSS伪类选择器
td:nth child(1)
,则会检索特定项,如
td:nth child(1)[href]
。我一直想写一篇关于支持什么和不支持什么的文章,以防有人想作为参考。当您选择切换到支持VBA的语言时,了解甚至不支持VBA的方法也很有用

在本例中,选择器通过以下方法应用:
HTMLDocument
。它以
nodeList
的形式返回所有匹配项,其
.Length
可以通过索引访问单个匹配元素

节点列表项:



参考资料(VBE>工具>参考资料):

  • Microsoft HTML对象库

  • 我会使用CSS选择器来定位链接,作为一种比启动浏览器更快的检索方法


    CSS选择器:

    以下是:

    td:first-child [href]
    
    td:第一个子元素是
    td
    标记元素的一部分<代码>“是一个,而
    []
    是一个。基本上,在本例中,它为每行的第一个
    td
    元素(即第一列)进行选择,然后选择其中的
    href
    属性元素

    :first-child CSS伪类表示 一组同级元素

    遗憾的是,VBA实现不支持选择器,因为确切的元素也可以与
    匹配。common tr+tr td:not([href*='rule'],br)
    。对伪选择器的支持非常有限。在这种情况下,如果子体组合中支持使用CSS伪类选择器
    td:nth child(1)
    ,则会检索特定项,如
    td:nth child(1)[href]
    。我一直想写一篇关于支持什么和不支持什么的文章,以防有人想作为参考。当您选择切换到支持VBA的语言时,了解甚至不支持VBA的方法也很有用

    在本例中,选择器通过以下方法应用:
    HTMLDocument
    。它以
    nodeList
    的形式返回所有匹配项,其
    .Length
    可以通过索引访问单个匹配元素

    节点列表项:



    参考资料(VBE>工具>参考资料):

  • Microsoft HTML对象库

  • 那是。。。太棒了。如此优雅。谢谢你QHarr,也谢谢你花时间给我解释(因为很多事情都是我无法理解的,就像一只蚂蚁在看一个巨人)。希望我能想出如何使它遍历一长串链接,并将结果输出到电子表格而不是debug.print。任何问题请告诉我。你可能想看看我的一些答案。他们中的许多人处理链接的循环列表,并提供了一个类的方法来进行抓取。。。太棒了。如此优雅。谢谢你QHarr,也谢谢你花时间给我解释(因为很多事情都是我无法理解的,就像一只蚂蚁在看一个巨人)。希望我能想出如何使它遍历一长串链接,并将结果输出到电子表格而不是debug.print。任何问题请告诉我。你可能想看看我的一些答案。他们中的许多人处理链接的循环列表,并提供一个类来进行抓取。