【问题标题】:Get the value inside Nested Tables and Divs using VBA使用 VBA 获取嵌套表和 Div 中的值
【发布时间】:2018-11-01 21:46:00
【问题描述】:

我已经尝试了多种解决方案,但仍未使代码正常工作。 我想将网站中的值复制/粘贴到 Excel 表中。 这是一些HTML:我所追求的值一直在底部。

<DIV style="HEIGHT:100%;WIDTH:100%" ID="oReportDiv">
    <DIV style="HEIGHT:100%;WIDTH:100%;" class="ap">
        <TABLE CELLSPACING="0" CELLPADDING="0">
            <TR>
                <TD ID="oReportCell">
                    <TABLE CELLSPACING="0" CELLPADDING="0">
                        <TR>
                            <TD class="a106xBc">
                                <DIV class="a106xB">
                                    <TABLE CELLSPACING="0" CELLPADDING="0" BORDER="0" COLS="9" LANG="en-US" class="r10">


                                        <TR VALIGN="top">

                                            <TD COLSPAN="3">
                                                <TABLE CELLSPACING="0" CELLPADDING="0" COLS="4" BORDER="0" style="border-collapse:collapse;" class="a103">


                                                    <TR VALIGN="top">
                                                        <TD style="HEIGHT:5.33mm;" class="a67c r14">&nbsp;</TD>
                                                        <TD style="" class="a71c">
                                                            <DIV style="word-wrap:break-word;text-decoration:none;" class="a71">Get This Value</DIV>
                                                        </TD>

<TR VALIGN="top">
                                                        <TD style="HEIGHT:5.33mm;" class="a67c r14">&nbsp;</TD>
                                                        <TD style="" class="a71c">
                                                            <DIV style="word-wrap:break-word;text-decoration:none;" class="a71">Second Value and so on</DIV>
                                                        </TD>

                                                    </TR>

这是我尝试过的错误: 对象不支持该属性或方法

Sub GrabLastNames()

'dimension (set aside memory for) our variables
Dim objIE As InternetExplorer
Dim ele As Object
Dim y As Integer
Dim div
Dim nodeList As Object, i As Long


'start a new browser instance
Set objIE = New InternetExplorer
'make browser visible
objIE.Visible = True





'navigate to page with needed data
objIE.navigate ""
'wait for page to load
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

'we will output data to excel, starting on row 1
y = 1

'look at all the 'tr' elements in the 'table' with id 'myTable',
'and evaluate each, one at a time, using 'ele' variable
Set nodeList = objIE.document.querySelectorAll("td.a71c .a71")

对于 i = 0 到 nodeList.Length - 1 Debug.Print nodeList.Item(i).innerText 下一个 对于 objIE.document.getElementById("oReportDiv").getElementsById("a71") 中的每个元素

    Debug.Print ele.textContent
    'each 'tr' (table row) element contains 4 children ('td') elements
    'put text of 1st 'td' in col A
    Sheets("Sheet3").Range("A" & y).Value = ele.Children(0).textContent
    'put text of 2nd 'td' in col B
    Sheets("Sheet3").Range("B" & y).Value = ele.Children(1).textContent
    'put text of 3rd 'td' in col C
    Sheets("Sheet3").Range("C" & y).Value = ele.Children(2).textContent
    'put text of 4th 'td' in col D
    Sheets("Sheet3").Range("D" & y).Value = ele.Children(3).textContent
    'increment row counter by 1
    y = y + 1
'repeat until last ele has been evaluated
Next

'save the Excel workbook
ActiveWorkbook.Save

结束子

感谢您的反馈

【问题讨论】:

  • 错误发生在哪一行代码?
  • @ArcherBird,对于 objIE.document.getElementById("oReportDiv").getElementsById("a71") 中的每个元素。我使用简单的 html 表测试了代码,它可以工作,但不能使用上面的 html
  • 也许可以试试...objIE.document.querySelector("div[class=oReportDiv] div[class=a71]")...
  • @ArcherBird 对象需要错误
  • 编辑:试试...objIE.document.querySelectorAll("div[class=oReportDiv] div[class=a71]")...

标签: html excel vba web-scraping


【解决方案1】:

试试

Dim nodeList As Object, i As Long
Set nodeList = objIE.document.querySelectorAll("td.a71c .a71")
For i = 0 To nodeList.Length -1
    Debug.Print nodeList.item(i).innerText
Next

或者

Set nodeList objIE.document.querySelectorAll("#oReportCell .a71") 

然后与上面相同的循环。

是否有要处理的父 iframe/frame 将是另一个想法。


完整的代码类似于:

Option Explicit

Public Sub GrabLastNames()
    Dim objIE As InternetExplorer, t As Date, nodeList As Object, i As Long
    Const MAX_WAIT_SEC As Long = 5
    Set objIE = New InternetExplorer

    With objIE
        .Visible = True
        .navigate "https://jetblue.rosterapps.com/GenerateReport.aspx?command=6Rqw0HG%2FJQvTea2EXxyC%2BSDRGUzjdE2GHNUaN5rcKVPdKRpHBhRkfYa3c%2FnPLk58WnTYk6kBq1OZHQYsWqgM8g%3D%3D&action=render"

        Do While .Busy = True Or .readyState <> 4: DoEvents: Loop

        t = Timer
        Do
            DoEvents
            On Error Resume Next
            Set nodeList = .document.querySelectorAll("#oReportCell .a71")
            On Error GoTo 0
            If Timer - t > MAX_WAIT_SEC Then Exit Do

        Loop While nodeList Is Nothing

        If Not nodeList Is Nothing Then
            With ThisWorkbook.Worksheets("Sheet3")
                For i = 0 To nodeList.Length - 1
                    .Cells(1, i + 1) = nodeList.item(i).innerText
                Next
            End With
        End If
        .Quit
    End With
End Sub

【讨论】:

  • 我是否摆脱了 For Each ele In 的循环?
  • 操作,没有粘贴整个 html。更新了它。它们是同一张表中的更多元素
  • 与其编辑你的问题,不如在 Do While objIE.Busy = True Or objIE.readyState 4: DoEvents: Loop 之后使用我的代码时发生了什么
  • 正确的值在哪里打印到即时窗口? (Ctrl + G)
  • 它粘贴了值,但我想如下:A1中的value1,A2中的value2等等
猜你喜欢
  • 2012-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-14
  • 2017-01-20
  • 2014-12-03
相关资源
最近更新 更多