【问题标题】:Only select rows, not header from a table in html只选择行,而不是从 html 中的表中选择标题
【发布时间】:2019-10-05 06:08:45
【问题描述】:

我正在尝试从网站上抓取表格,我的最终输出必须是来自第一列的表格数据。

表格结构如下图:

我感兴趣的行位于 rowalt 类下。 运行下面的代码还会得到三个不需要的单元格,一个来自第一行align = "right",一个来自第二行class="gna",一个来自最后一行,其配置与第一行完全相同 - align = "right"

wb := ComObjCreate("InternetExplorer.Application")
wb.Visible := True
wb.Navigate("C:\Users\Marian\Downloads\webpage.htm")
; Wait for page to load:
While wb.Busy or wb.ReadyState != 4
    Sleep, 100

Table := wb.document.getElementById("gvSearchResults")
Rows := Table.rows
Loop % rows.length 
{
    cells := rows[A_Index-1].cells

        out .= cells["0"].innerText ","

    out := RTrim(out,",") "`n"
}
Msgbox, %out%

如何将更多过滤器添加到输出中,以便将结果限制在所需的单元格中?谢谢!

LE:我认为getElementsbyClassnamequeryselectorall() 不起作用,因为该网页的html 协议不支持它们。

html代码以:

开头
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0021)http://www.google.com -->  ;This was adapted according to the comment section
<HTML><HEAD><META content="IE=7.0000" http-equiv="X-UA-Compatible">
<TITLE>ECSD |>> Files</TITLE>

【问题讨论】:

    标签: html autohotkey


    【解决方案1】:

    我会使用querySelectorAll 来获取所有.row.alt 行。 之后,您可以跳过循环中的最后一行

    rows := wb.document.querySelectorAll("#gvSearchResults tr.row, #gvSearchResults tr.alt")
    Loop % rows.length - 1   ;-1 added to skip the last row 
    {
        cells := rows[A_Index-1].cells
    
            out .= cells["0"].innerText ","
    
        out := RTrim(out,",") "`n"
    }
    Msgbox, %out%
    

    编辑基于 cmets。 有问题的页面有 &lt;META content="IE=7.0000" http-equiv="X-UA-Compatible"&gt; 标签,它强制 IE 在 IE7 兼容模式下运行。 IE7 不支持querySelectorAll

    当 IE 在 IE7 模式下运行时,此解决方案也有效。它不是很灵活,因为您必须事先知道需要跳过哪些行。

    wb := ComObjCreate("InternetExplorer.Application")
    wb.Visible := True
    wb.Navigate("C:\Users\Marian\Downloads\webpage.htm")
    ; Wait for page to load:
    While wb.Busy or wb.ReadyState != 4
        Sleep, 100
    
    Table := wb.document.getElementById("gvSearchResults")
    Rows := Table.rows
    Loop % rows.length - 1 ;-1 added to skip the last row
    {
       if (A_index = 1 OR A_index = 2) ;skip the first and the second iteration of the loop, effectively skipping the first and the second row of the table;
       continue   
    
       cells := rows[A_Index-1].cells
       out .= cells["0"].innerText ","
       out := RTrim(out,",") "`n"
    }
    Msgbox, %out%
    

    【讨论】:

    • 为了简化,这是页面中唯一的表格。我不熟悉Document 方法QuerySelector() 并且在尝试您的代码时遇到unknown name 错误。 T
    • 另外,当我说最后一行配置为第一行时,我指的是第一行有allign = "right"
    • 我刚刚有机会测试代码,在 IE11 和模型表上运行良好。你能告诉我你如何声明wb
    • 查看更新后的问题,包括所有的修改。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    相关资源
    最近更新 更多