Python 如何从 Basic Table 获取 Scrapy Xpath 数据?

Python How to get Scrapy Xpath data from Basic Table?

<TABLE>
<br>

    <TR>
    <td width = 270><p align="left" style="margin-left: 0;"><b>Info</b></p></td>
    <td><p>  </p></td>
    </TR>
    <TR>
    <td width = 270><p align="left" style="margin-left: 10;">Page&nbsp;Count</p></td>
    <td><p> =  4 </p></td>
    </TR>
    ...

正在尝试从上面的 table 中获取 = 4 值的 response.xpath。即使在检查 Chrome 中的元素并以这种方式拉动 xpath 时,我仍然会得到一个 [] 值。尝试过:

/html/body/table[1]/tr[2]/td[2] 
//table[2]/tr[2]/td[2] 

都失败了。

我会通过 Count 文本获取 td,然后获取 following-sibling:

//td[contains(p, "Count")]/following-sibling::td/p/text()

演示:

$ scrapy shell index.html
In [1]: response.xpath('//td[contains(p, "Count")]/following-sibling::td/p/text()').extract()
Out[1]: [u' = 4 ']

如果要提取实际数字,请使用 .re():

In [2]: response.xpath('//td[contains(p, "Count")]/following-sibling::td/p/text()').re(r'(\d+)')
Out[2]: [u'4']