如何遍历一系列单元格并从网站获取它们的值?

问题描述

我试图从我的工作场所使用的网站获取一些特定数据,但是,我无法弄清楚如何将一些特定数据写入工作表。我写了一段代码,成功打开网页并循环遍历该页面上的数据,我只需要最后一部分代码的帮助。

Dim Source As Range
        Dim Cell As Range

lastRow = Worksheets("Sheet1").Range("A1000").End(xlUp).Row
Set Source = Worksheets("Sheet1").Range("A2:A" & lastRow)

For Each Cell In Source


Dim nodes As Object,i2 As Long

Set nodes = objIE.Document.querySelectorAll("[Title='Purchase Order / Status']")

For i2 = 0 To nodes.Length - 1

    If nodes.Item(i2).innerText Like "*" & Cell.Value & "*" Then
        Cell.Offset(0,3).Value = ' Im missing the code here
        Exit For
    End If

Next
Next

我遗漏了代码的最后一部分(在上面的代码标记为遗漏)。此代码循环遍历我的工作表,然后遍历整个网页,并查找标题为“采购订单/状态”的数据。这部分代码没问题。

然后我尝试了 Cell.Offset(0,3).Value = objIE.Document.querySelector("[Title='Planned Destinations ']").innerText,它成功获取了我需要的“计划目的地”内文值。但是,它总是在整个页面获取一个找到的“计划目的地”内部文本。

这是它的功能以及我想要它做什么的图片

Example

注意 D 列。

这是网站的一部分:

<tr class="outboundplanAltRowStyle">
<td class="outboundplanHour" style="height:25px;width:40px;white-space:Nowrap;">11:00</td>
 <td onmouSEOver="this.className='outboundplanHover'" onmouSEOut="this.className=''" 
     onclick="cellClicked(1019543,14)" 
     style="height:25px;width:150px;white-space:Nowrap;" class="">
<table class="outboundplan_PREBOOKED" style="width: 200px; table-layout: fixed" cellpadding="0px" cellspacing="0px">
   <tbody><tr>
    <td title="Purchase Order / Status" class="outboundCell"> 325839 / PREBOOKED</td></tr>
    <tr><td title="Subcontractor Name / Load Numbers " 
               class="outboundCell">Tesco FM /  - </td></tr>
    <tr><td title="Planned Destinations " class="outboundCell" 
       style="overflow: hidden"> 39019 (NDC Teresin)&nbsp;</td></tr>
    <tr><td title="Status Date" class="outboundCell">28.01.2021 12:02&nbsp;</td></tr></tbody>
</table></td>

我需要通过查找 title="Purchase Order/Status" 内文本将 title="Planned Destinations" 内文本值添加到单元格中,该内文本保存在工作表中(上图中的 A 列)

编辑:

终于成功了。对我的问题使用了不同的方法,但如果没有蒂姆的提示和帮助,我将无法完成它。这是我的问题的最终工作代码,以防万一有人需要它或灵感:

Dim objResultList As MSHTML.IHTMLDOMChildrenCollection
        Dim lResultCount As Long
        Dim lResultLoop As Long
        Dim anchorLoop As MSHTML.HTMLAnchorElement

Set objResultList = objIE.Document.querySelectorAll("[Title='Planned Destinations ']")
    
    lResultCount = objResultList.Length

    Debug.Print
    
    For lResultLoop = 0 To lResultCount - 1
        
        Set anchorLoop = objResultList.Item(lResultLoop)
     
    Next
        
    i3 = 0
    Set Source = Worksheets("Sheet1").Range("D2:D" & lastRow)
    If Not anchorLoop Is nothing Then
    For Each Cell In Source
    Cell.Value = objResultList.Item(i3).innerText
    i3 = i3 + 1
    
    Next
    End If
    End If

解决方法

您需要从 nodes.Item(i2)“向上”到父表(例如使用 parentElement),然后查询该表(而不是整个文档)以获取您想要的其他单元格:

Sub tester()
    'Added reference to Microsoft HTML Object Model
    Dim doc As New HTMLDocument,nodes As Object,i As Long
    Dim tbl As HTMLTable,nxt
    
    doc.body.innerHTML = Range("A1").Value 'load HTML from cell for testing

    Set nodes = doc.querySelectorAll("td [Title='Purchase Order / Status']")
    Debug.Print "found " & nodes.Length & " cell(s)"
    
    For i = 0 To nodes.Length - 1
    
        'get the parent table
        Set tbl = nodes(i).parentElement.parentElement.parentElement
        'find the other cell in this table
        Set nxt = tbl.querySelector("td [Title='Planned Destinations ']")
        
        Debug.Print nodes(i).innerText,nxt.innerText
        
    Next i
    
End Sub

A1 中的 HTML:

<table><tr>
      <td>11:00</td>
     <td>
             <table class="outboundPlan_PREBOOKED">
            <tbody>
                    <tr><td title="Purchase Order / Status" class="outboundCell"> 325839 / PREBOOKED</td></tr>
                   <tr><td title="Subcontractor Name / Load Numbers "  class="outboundCell">Tesco FM /  - </td></tr>
                   <tr><td title="Planned Destinations " class="outboundCell" >39019 (NDC Teresin)&nbsp;</td></tr>
                   <tr><td title="Status Date" class="outboundCell">28.01.2021 12:02&nbsp;</td></tr>
            </tbody>
            </table></td>
</tr></table>