我创建了一个xpath表达式来定位元素,以便可以在scrapy中使用xpath从某些html元素中提取某些信息。无论如何我都无法达到。
HTML元素:
<div class="rates">
<label>
Rates :
</label>
R 3500
<br class="hidden-md hidden-lg">
</div>
我希望从中提取R 3500
出来。
我尝试过:
from scrapy import Selector
html = """
<div class="rates">
<label>
Rates :
</label>
R 3500
<br class="hidden-md hidden-lg">
</div>
"""
sel = Selector(text=html)
rate = sel.xpath("//*[@class='rates']/label/following::*").get()
print(rate)
运行上面的脚本后,这就是我想要的,<br class="hidden-md hidden-lg">
而我希望得到R 3500
。
.tail
如果选择我可以使用lxml
。但是,当我抓狂时,找不到任何类似的东西。
如何使用xpath从html元素中提取该比率?
要将文本节点作为节点following-sibling
之后的label
节点:
...
sel = Selector(text=html)
rate = sel.xpath("//*[@class='rates']/label/following-sibling::text()").get().strip()
print(rate)
输出:
R 3500
另外:"//*[@class='rates']/label/following::text()"
应该也可以。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句