【问题标题】:XPath help needed for row text search column return on MorningStarMorningStar 上的行文本搜索列返回需要 XPath 帮助
【发布时间】:2017-12-08 23:09:43
【问题描述】:

需要从 Html 中提取一些数据,如下所示:

<div class="r_tbar0 positionrelative">
    <h3>Financials</h3>
</div>
<table class="r_table1 text2" cellspacing="0" cellpadding="0">
    <thead>
        <tr>
            <th scope="row" align="left"></th>
            <th scope="col" id="Y0" align="right">2007-12</th>
            <th scope="col" id="Y1" align="right">2008-12</th>
            <!--More columns here-->
            <th scope="col" id="Y9" align="right">2016-12</th>
            <th scope="col" id="Y10" align="right">TTM</th>
        </tr>
    </thead>
    <tbody>
        <tr class="hr">
            <td colspan="12"></td>
        </tr>
        <tr>
            <th class="row_lbl" scope="row" id="i0">Revenue&nbsp;<span>USD Mil</span></th>
            <td headers="Y0 i0" align="right">5,858</td>
            <td headers="Y1 i0" align="right">5,808</td>
            <!--More cells here-->
            <td headers="Y9 i0" align="right">4,272</td>
            <td headers="Y10 i0" align="right">4,955</td>
        </tr>
        <tr class="hr">
            <td colspan="12"></td>
        </tr>
        <tr>
            <th class="row_lbl" scope="row" id="i1">Gross Margin %</th>
            <td headers="Y0 i1" align="right">37.4</td>
            <td headers="Y1 i1" align="right">39.9</td>
            <!--More cells here-->
            <td headers="Y9 i1" align="right">23.4</td>
            <td headers="Y10 i1" align="right">33.5</td>
        </tr>
        <!--More rows here-->
        <tr class="hr">
            <td colspan="12"></td>
        </tr>
    </tbody>
</table>

我希望通过搜索“收入”行然后查看 2007 列来从关键比率页面中提取 2007 年收入数据 XPATH。

XPATH 2007 年收入的位置:

//*[@id="financials"]/table/tbody/tr[2]/td[1]

tr[2] 表示与 Revenue 对齐的行。但是,如果我有一个查看多只股票的程序,我想确保 tr[2] 仍然查看收入。

我尝试了以下 XPATH 的多个版本,它返回 NULL 值。 (我正在使用 XPATH 助手作为谷歌浏览器扩展)

//*[@id="financials"]/table/tbody/tr[contains(text(),'Revenue')]/td[1]

收入行的外部 html 代码:

<th class="row_lbl" scope="row" id="i0">Revenue&nbsp;<span>USD Mil</span></th>

2007 年收入的外部 html 代码:

<td align="right" headers="Y0 i0" class="">5,858</td>

更新

根据我写的以下答案:

//*[@id='financials']//td[contains(@headers,'i0')][1]

提取 2017 年收入数据 5,858

【问题讨论】:

  • 今晚晚些时候我会编辑这个,请不要删除。

标签: python-3.x xpath web-scraping finance


【解决方案1】:

在“财务”表中,“收入”是 th,而不是 tr。您可以通过引用td 标签的header 属性来获取该表的列或行中的所有单元格。列是Y0..Yn,行是i0..in,例如:

第一列有标题Y0:

//*[@id='financials']//td[contains(@headers,'Y0')]

第一行有标题i0:

//*[@id='financials']//td[contains(@headers,'i0')]

等等

【讨论】:

  • 谢谢 derloopkat,在您的帮助下,我找到了我正在寻找的答案://*[@id='financials']//td[contains(@headers,'i0' )][1]
猜你喜欢
  • 1970-01-01
  • 2013-12-26
  • 2017-06-07
  • 1970-01-01
  • 1970-01-01
  • 2017-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多