【问题标题】:Get HTML Table content with VBA for Excel using arrays使用数组使用 VBA for Excel 获取 HTML 表格内容
【发布时间】:2021-07-15 01:04:30
【问题描述】:

试图从本网站的第二个表格中获取数据,因为第一个表格只包含下拉列表的元素,无论出于何种原因,它都作为表格包含在 HTML 中!

代码引用了一个类似的页面,但是,其中第一个表不存在,它工作正常,只是不在具有两个具有不同内容的表的页面上。

所以想法是使用下面的代码,但首先跳过第一个表并仅提取与给定数组中的元素匹配的第二个表 (tr/td) 的内容。

有谁知道如何修改代码来处理这个问题?谢谢!

两个表的片段(运行 sn-p 以查看下拉列表):

<table border="1">
  <tbody>
   <tr>
    <td>
    <select size="1" onchange="nextpage(this.options[this.selectedIndex].value,'-1','-1')">
    <option value="1-1-11">1-2</option>
    <option value="all" selected="selected">all</option>
    </select>
    </td>
    <td></td>
   </tr>
  </tbody>
 </table>
<table border="0">   
 <tbody>
  <tr>
   <td>valign=“top“ aling“left“>
    <nobr>Description</nobr></td>

包含函数的代码部分

Dim table As MSHTML.HTMLTable, R As Long, c As Long, headers(), row As MSHTML.HTMLTableRow
Dim results() As Variant, html2 As MSHTML.HTMLDocument

headers = Array("HDR01", " HDR02", " HDR03", " HDR04")

ReDim results(1 To 100, 1 To UBound(headers) + 1)

    Set table = html.querySelector("table")
    Set html2 = New MSHTML.HTMLDocument

    Dim lastRow As Boolean
  
 For Each row In table.Rows

       Dim header As String
       lastRow = False

        html2.body.innerHTML = row.innerHTML
        header = Trim$(row.Children(0).innerText)        

        If header = "Description" Then          
            R = R + 1
            Dim dict As Scripting.Dictionary: Set dict = GetBlankDictionary(headers)
        End If

        If dict.Exists(header) Then 
           dict(header) = Trim$(row.Children(1).innerText)       
        End If        

        ....

        If lastRow Then
            populateArrayFromDict dict, results, R
        End If

 Next
 
 With ActiveSheet
    .Cells(1, 1).Resize(1, UBound(headers) + 1) = headers
    .Cells(2, 1).Resize(UBound(results, 1), UBound(results, 2)) = results
 End With

功能:

Public Function GetBlankDictionary(ByRef headers() As Variant) As Scripting.Dictionary
    Dim dict As Scripting.Dictionary, i As Long

    Set dict = New Scripting.Dictionary

    For i = LBound(headers) To UBound(headers)
        dict(headers(i)) = vbNullString
    Next

    Set GetBlankDictionary = dict
End Function

我需要这样的东西:

  If table.Border = "1" Then   'with Droplist
    Set table = html.querySelectorAll("body").Item(1)   'skip table0
    ElseIf table.Border = "0" Then  'wihtout Droplist
    Set table = html.querySelectorAll("body").Item(0)   'start with this table
    End If

【问题讨论】:

    标签: arrays excel vba web-scraping html-table


    【解决方案1】:

    将正确的属性和值添加到选择器中以获得正确的表格

    Set table = html.querySelector("table[border='0']")
    

    【讨论】:

    • 我确实循环了表格只是为了看看那里有什么,并且存在 3 个表格,只有所需的一个有边框 = 0。所以这段代码很好用!现在存在多页返回以及如何移动到其他页面以继续检索记录的问题。无论如何,这可能需要遍历表以至少从位于第一个表中的页面导航器中获取页数。除非也有一种缩写的方式来引用它。听起来 Jasco 很快就会发布另一个问题。
    • @Qharr:又是典型的……使用一条代码线,您就可以让魔鬼用他的三叉戟蒸发。祝你们都是 A :-)
    • @June7: 三张桌子??? Qharr 的代码适用于多页,我会检查它是否也适用于 Access...实际上它应该!
    • @Jasco,我用“Celle”进行了测试,它只提取了 11 条记录中的 10 条。第 11 条记录在第二页。是的,3 个带有“Celle”的表格,其中 2 个是页面导航下拉菜单。我在我们的聊天中添加了很多 cmets。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    • 2013-12-10
    • 2012-01-03
    • 2020-07-14
    • 2018-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多