【问题标题】:how to find location of specific <tr> each time code is run每次运行代码时如何查找特定 <tr> 的位置
【发布时间】:2018-05-29 17:36:30
【问题描述】:

我下面的代码将为一天中的每个小时提取一个值。

但是,我正在抓取的网页可能会发生变化,因此我想找到一种方法将 的位置分配给变量,以便它每次都知道它是什么数字。我通过反复试验找到了当前的数字“116”。

我也包含了下面的 html 结构。有什么建议?

Sub scrape()
    Dim IE As Object
    Set IE = CreateObject("InternetExplorer.application")
    With IE
        .Visible = False
        .navigate "web address"
        Do Until .readyState = 4
            DoEvents
        Loop
        .document.all.item("Login1_UserName").Value = "user"
        .document.all.item("Login1_Password").Value = "pw"
        .document.all.item("Login1_LoginButton").Click
        Do Until .readyState = 4
            DoEvents
        Loop
    End With

    Dim htmldoc As Object
    Dim r
    Dim c
    Dim aTable As Object
    Dim TDelement As Object
    Set htmldoc = IE.document

    Dim td As Object
    For Each td In htmldoc.getElementsByTagName("td")
        On Error Resume Next
        If span.Children(0).id = "ctl00_PageContent_grdReport_ctl08_Label50" Then
            ThisWorkbook.Sheets("sheet1").Range("j8").Offset(r, c).Value = td.Children(1).innerText
        End If
        On Error GoTo 0
    Next td
End Sub

HTML:

<form name="aspnetForm" id="aspnetForm" action="./MinMaxReport.aspx" 
method="post">
<div>
</div>
<script type="text/javascript">...</script>
<div>
</div>
<table class="header-table">...</table>
<table class="page-area">              
<tbody>
<tr>
<table id="ctl00_PageContent_Table1" border="0">...</table>
<table id="ctl00_PageContent_Table2" border="0">
<tbody>
<tr>
<td>
<div id="ctl00_PageContent_grdReport_div">
<tbody>
<tr style="background-color: beige;">
<td>...</td>
<td>
<span id="ctl00_PageContent_grdReport_ctl08_Label50">Most Restrictive 
Capacity Maximum</span>
</td>
<td>
<span id="ctl00_PageContent_grdReport_ctl08_Label51">159</span>
</td>                                     
</tr>		
</tbody>
</div>
</td>
</tr>
</tbody>
</table>
</table>
</tr>
</tbody>
</table>

【问题讨论】:

    标签: html vba excel getelementbyid getelementsbytagname


    【解决方案1】:

    您可以遍历所有 TD 并检查 id= "ctl00_PageContent_grdReport_ctl08_Label50" 例如:

    For Each td In htmldoc.getElementsByTagName("td")
        On Error Resume Next
            If td.Children(0).ID = "ctl00_PageContent_grdReport_ctl08_Label50" Then
                ThisWorkbook.Sheets("sheet1").Range("j8").Offset(r, c).Value = td.Children(1).innerText
            End If
        On Error GoTo 0
    Next td
    

    Children(0) 将选择表格单元格中包含的第一个 iHTML 元素。 On Error Resume Next 适用于 td 元素没有子元素的情况。 您的网页中可能有多个具有此 ID 的元素。然后,您必须首先识别表或表行。我不能这样做,因为我看不到你的整个 HTML 代码。

    【讨论】:

    • 感谢 Marcin,但是当我使用您建议的循环运行我的代码时,它没有返回任何内容。我还尝试用“span”替换“td”并得到相同的结果。还有什么建议吗?
    • 你能在你网站的源码中按Ctrl+F找到这个字符串吗:ctl00_PageContent_grdReport_ctl08_Label50
    • 是的,我可以在源代码中使用 Ctrl+F 找到它。
    • 我发现使用 childnodes.item(0).id 会返回数据,但使用 children(0).id 不会返回数据。但是,使用子节点返回的数据仍然没有返回“ctl00_PageContent_grdReport_ctl08_Label50”的内部文本。
    • 我更新了显示整个 html 的 html 代码。
    猜你喜欢
    • 2019-06-19
    • 2011-03-24
    • 1970-01-01
    • 2022-12-11
    • 1970-01-01
    • 2013-04-18
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多