【问题标题】:Excel VBA Web Scraping Table Elements from a <frameset> and a <frame>Excel VBA Web 从 <frameset> 和 <frame> 抓取表格元素
【发布时间】:2019-07-02 23:23:50
【问题描述】:

我正在尝试将一些看起来像表格的项目从网站抓取到 Excel 中。

我对一般的编码并不陌生,虽然我对 Excel 意义上的 VBA 很陌生 :)

我尝试过使用 Excel 的数据>从 Web 界面,它无法识别表格。我猜这是因为它是使用构建的(或者至少这是我的 Google-Fu 让我理解的)。

Snipping of what the second table looks like

<html>

<frame title="links" ...>...</frame>

<frame title="queue">
#document

<head>...</head>
<body>
<div id="container>
<script>...</script>
<div>

<table id="oTable">

<colgroup>...</colgroup>

<thead>...</thead>


<tbody>
  <tr onclick="changeHighlight( 'eid0' )" id="eid0" class="queryshaded">
    <td nowrap=""><a onclick="javascript:window.open('IWViewer.jsp?id=3.5599976.5599976');" title="Open Image" href="javascript:doNothing();"><img title="Open Image" border="0" alt="Open Image" src="URL.gif"></a>&nbsp;<a onclick="javascript:window.open('URL','_newtab');" title="Open Workitem" href="javascript:doNothing();"><img title="Open Workitem" border="0" alt="Open Workitem" src="URL.gif"></a>
    </td><td scope="row" nowrap=""><a href="URL" target="_Blank">12345</a></td>
    <td nowrap=""><a href="`" target="_Blank">28/08/2018 17:00:49</a></td>
    <td nowrap=""><a href="URL" target="_Blank">11/09/2018 16:28:39</a></td>
    <td nowrap=""><a href="URL" target="_Blank">5,599,976</a></td>
    <td nowrap=""><a href="URL" target="_Blank">dijm</a></td></tr>
  <tr onclick="changeHighlight( 'eid1' )" id="eid1" class="queryunshaded">
    <td nowrap=""><a onclick="javascript:window.open('IWViewer.jsp?id=3.6443276.6443276');" title="Open Image" href="javascript:doNothing();"><img title="Open Image" border="0" alt="Open Image" src="URL.gif"></a>&nbsp;<a onclick="javascript:window.open('URL;id=3.6443276.6443276','_newtab');" title="Open Workitem" href="javascript:doNothing();"><img title="Open Workitem" border="0" alt="Open Workitem" src="URL.gif"></a>
    </td><td scope="row" nowrap=""><a href="URL" target="_Blank">67890</a></td>
    <td nowrap=""><a href="URL" target="_Blank">25/06/2019 11:01:01</a></td>
    <td nowrap=""><a href="URL" target="_Blank">09/07/2019 10:32:32</a></td>
    <td nowrap=""><a href="URL" target="_Blank">6,443,276</a></td>
    <td nowrap=""><a href="URL" target="_Blank"></a></td></tr>
  <tr onclick="changeHighlight( 'eid2' )" id="eid2" class="queryshaded">
    <td nowrap=""><a onclick="javascript:window.open('IWViewer.jsp?id=3.6443287.6443287');" title="Open Image" href="javascript:doNothing();"><img title="Open Image" border="0" alt="Open Image" src="URL.gif"></a>&nbsp;<a onclick="javascript:window.open('URL;id=3.6443287.6443287','_newtab');" title="Open Workitem" href="javascript:doNothing();"><img title="Open Workitem" border="0" alt="Open Workitem" src="URL.gif"></a>
    </td><td scope="row" nowrap=""><a href="URL" target="_Blank">23456</a></td>
    <td nowrap=""><a href="URL" target="_Blank">25/06/2019 11:01:24</a></td>
    <td nowrap=""><a href="URL" target="_Blank">09/07/2019 10:35:30</a></td>
    <td nowrap=""><a href="URL" target="_Blank">6,443,287</a></td>
    <td nowrap=""><a href="URL" target="_Blank"></a></td></tr>
  <tr onclick="changeHighlight( 'eid3' )" id="eid3" class="queryunshaded">
    <td nowrap=""><a onclick="javascript:window.open('IWViewer.jsp?id=3.6443339.6443339');" title="Open Image" href="javascript:doNothing();"><img title="Open Image" border="0" alt="Open Image" src="URL.gif"></a>&nbsp;<a onclick="javascript:window.open('URL;id=3.6443339.6443339','_newtab');" title="Open Workitem" href="javascript:doNothing();"><img title="Open Workitem" border="0" alt="Open Workitem" src="URL.gif"></a>
    </td><td scope="row" nowrap=""><a href="URL" target="_Blank">78901</a></td>
    <td nowrap=""><a href="URL" target="_Blank">25/06/2019 11:06:02</a></td>
    <td nowrap=""><a href="URL" target="_Blank">09/07/2019 10:40:39</a></td>
    <td nowrap=""><a href="URL" target="_Blank">6,443,339</a></td>
    <td nowrap=""><a href="URL" target="_Blank"></a></td></tr>
  <tr onclick="changeHighlight( 'eid4' )" id="eid4" class="queryshaded">
    <td nowrap=""><a onclick="javascript:window.open('IWViewer.jsp?id=3.6443344.6443344');" title="Open Image" href="javascript:doNothing();"><img title="Open Image" border="0" alt="Open Image" src="URL.gif"></a>&nbsp;<a onclick="javascript:window.open('URL;id=3.6443344.6443344','_newtab');" title="Open Workitem" href="javascript:doNothing();"><img title="Open Workitem" border="0" alt="Open Workitem" src="URL.gif"></a>
    </td><td scope="row" nowrap=""><a href="URL" target="_Blank">34567</a></td>
    <td nowrap=""><a href="URL" target="_Blank">25/06/2019 11:06:17</a></td>
    <td nowrap=""><a href="URL" target="_Blank">09/07/2019 10:40:43</a></td>
    <td nowrap=""><a href="URL" target="_Blank">6,443,344</a></td>
    <td nowrap=""><a href="URL" target="_Blank"></a></td></tr>

我尝试了各种看起来像这样的解决方案: https://www.ozgrid.com/forum/forum/other-software-applications/excel-and-web-browsers-help/131683-extracting-data-from-a-grid-on-webpageScraping data from website using vba

并尝试自己定义框架以尝试从那里获取信息? (再次:Excel VBA 新手)

    'set myHTMLDoc to the main pages IE document
    Dim myHTMLDoc As HTMLDocument
    Set myHTMLDoc = ie.Document

    'set myHTMLFrame2 as the 2nd frame of the main page (index starts at 0)
    Dim myHTMLFrame2 As HTMLDocument
    Set myHTMLFrame2 = myHTMLDoc.Frames(1).Document

使用上面的代码块,我收到“运行时错误 '438' 如果没有上述块,我会收到“运行时错误'1004'

我最终想要的信息在每一行中:

    </td><td scope="row" nowrap=""><a href="URL" target="_Blank">67890</a></td>
    <td nowrap=""><a href="URL" target="_Blank">25/06/2019 11:01:01</a></td>
    <td nowrap=""><a href="URL" target="_Blank">09/07/2019 10:32:32</a></td>
    <td nowrap=""><a href="URL" target="_Blank">6,443,276</a></td>

理想情况下,我想将每个元素转储到一个单元格中

67890 | 25/06/2019 11:01:01 | 09/07/2019 10:32:32 | 6,443,276

每页有 20 行(有一个按钮可以进入下一页,我稍后会弄清楚...希望哈哈)

非常感谢任何可以提供帮助的人:)

-编辑- 这是我目前正在使用的代码(并不珍贵:P)

Private Sub CommandButton1_Click()


    Dim ie     As Object
    Dim html   As Object
    Dim objElementTR As Object
    Dim objTR  As Object
    Dim objElementsTD As Object
    Dim objTD  As Object
    Dim result As String
    Dim intRow As Long
    Dim intCol As Long

    Set ie = CreateObject("InternetExplorer.Application")
    ie.Navigate "URL"
    ie.Visible = True     ' loop until page is loaded
    Do Until (ie.ReadyState = 4 And Not ie.Busy)
        DoEvents
    Loop

    'set myHTMLDoc to the main pages IE document
    Dim myHTMLDoc As HTMLDocument
    Set myHTMLDoc = ie.Document

    'set myHTMLFrame2 as the 2nd frame of the main page (index starts at 0)
    Dim myHTMLFrame2 As HTMLDocument
    Set myHTMLFrame2 = ie.Document.querySelector("[title=queue]").contentDocument.getElementById("oTable")

    result = myHTMLFrame2
    Set html = CreateObject("htmlfile")
    myHTMLFrame2 = result
    Set objElementTR = html.getElementsByTagName("tr")
    ReDim myarray(0 To objElementTR.Length, 0 To 10)
    For Each objTR In objElementTR
        intRow = intRow + 1
        Set objElementsTD = objTR.getElementsByTagName("td")
        For Each objTD In objElementsTD
            myarray(intRow, intCol) = objTD.innerText
            intCol = intCol + 1
        Next objTD
        intCol = 0
    Next objTR
    With Sheets(1).Cells(1, 1).Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
        .Resize(UBound(myarray), UBound(myarray, 2)).Value = myarray
    End With



End Sub

【问题讨论】:

  • 网址是什么?

标签: html excel vba web-scraping


【解决方案1】:

您可以尝试通过其标题属性隔离框架,然后通过 contentDocument 并通过 id 获取表格

ie.document.querySelector("[title=queue]").contentDocument.querySelector("#oTable")

那么结束.querySelector("#oTable")可以和.getElementById("oTable")互换

然后我会转储表via clipboard.outerHTML,以便将表直接粘贴到工作表中。

【讨论】:

  • 当我将文档类型设置为 11 时,ie.Document.querySelector("[title=queue]").contentDocument.querySelector("#oTable") 不会产生错误。我注释掉了myHTMLFrame2 = result,它一直运行到.Resize(UBound(myarray), UBound(myarray, 2)).Value = myarray,然后才给出运行时错误“1004”,尽管它跳过整个 For 循环。
  • 我的代码已选择您的表。你看到我回答的另一部分了吗?链接?
  • 我做了,现在是睡觉时间,我明天试试。非常感谢您帮助我并如此迅速:)
  • 不用担心。如果有其他问题,请告诉我。但是,是的,今天的就寝时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-18
  • 2018-03-07
  • 1970-01-01
相关资源
最近更新 更多