【问题标题】:YQL xpath not robust enoughYQL xpath 不够健壮
【发布时间】:2016-12-06 13:32:32
【问题描述】:

以前使用 python 工作 n xpath 并且从网页中提取数据很健壮。现在我需要对同一个网页使用 YQL,但它不够健壮。

我想得到的是 1. 最后(澳元) 2. 关闭 3.收盘(%) 4. 累计成交量 来自https://www.shareinvestor.com/fundamental/factsheet.html?counter=TPM.AX 我在python中使用的xpath如下:

xpath('//td[contains(., "Last")]/strong/text()')
xpath('//td[contains(., "Change")]/strong/text()')[0]
xpath('//td[contains(., "Change (%)")]/strong/text()')
xpath('//td[contains(., "Cumulative Volume")]/following-sibling::td[1]/text()')

部分html在这里

<tr>
                <td rowspan="2" class="sic_lastdone">Last (AUD): <strong>6.750</strong></td>
                <td class="sic_change">Change: <strong>-0.080</strong></td>
                <td>High: <strong>6.920</strong></td>
                <td rowspan="2" class="sic_remarks">
                  Remarks: <strong>-</strong>
                </td>
              </tr>
              <tr>
                <td class="sic_change">Change (%): <strong>-1.17</strong></td>
                <td>Low: <strong>6.700</strong></td>
              </tr>
              <tr>

<tr>
                <td>Cumulative Volume (share)</td>
                <td class='sic_volume'>3,100,209</td>
                <td>Cumulative Value</td>
                <td class='sic_value'></td>
              </tr>

但是当我想申请YQL时,它没有工作。它只适用于

select * from html where
url="https://www.shareinvestor.com/fundamental/factsheet.html?counter=TPM.AX"
and xpath="//td/strong"

它将获取大量数据。我想要一个特定的数据并且需要健壮,以便网页的更改,我的查询仍然有效。如何获得健壮的 YQL xpath?

【问题讨论】:

    标签: python xpath yql


    【解决方案1】:

    您应该避免根据可见文本构建 xpath。

    我总是根据标签属性构建 xpath,因为它们通常不会改变。这使得 xpath 结果是唯一的,并且不受 HTML 中可见文本更改的影响。

    例如,“Last (AUD):”值 xpath: //td[@class="sic_lastdone"]/strong/text()

    【讨论】:

    • 只要找出和你一样的 xpath 答案。谢谢。属性标签很少改变吗?
    • 是的,它们本质上类似于 ID。文本可能会改变(多语言支持网站),但属性很少改变,因为它们不可见
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多