ROW TEXT搜索列在Morningstar上返回所需的XPath帮助



需要从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列。

X Path的位置2007收入:

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

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

我尝试了以下Xpath的多个版本,该版本返回了null值。(我使用的是XPath Helper A Google Chrome扩展名)

//*[@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>

update

基于以下答案,我写了:

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

提取2017年收入数据5,858

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

第一列的标题Y0

//*[@ID ='Financials']//td [contains(@headers,'y0')]

第一行具有标题i0

//*[@ID ='Financials']//TD [contains(@headers,'i0')]

等等

最新更新