【问题标题】:Python How to get Scrapy Xpath data from Basic Table?Python 如何从基本表中获取 Scrapy Xpath 数据?
【发布时间】:2015-03-16 20:04:19
【问题描述】:
<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>
    ...

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

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

都失败了。

【问题讨论】:

  • 由于您显示的 HTML 的 sn-p 有限,无法知道它失败的原因。为什么要同时尝试[1][2] 作为表格元素的位置?

标签: python xpath scrapy


【解决方案1】:

我会改为通过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']

【讨论】:

    猜你喜欢
    • 2014-11-07
    • 1970-01-01
    • 2019-03-17
    • 1970-01-01
    • 2014-10-04
    • 2015-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多