gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:47:42 24 4
gpt4 key购买 nike

<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]

都失败了。

最佳答案

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

关于Python 如何从Basic Table获取Scrapy Xpath数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29085948/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com