试图从www.currys.co.uk那里抢走智能手表的重量。该网站并不遵循所有产品的相同结构,所以为了获得每个产品的权重,我尝试使用xpath的关键字搜索
//text()[contains(.,'Weight')]问题出在代码中,我可以得到文本“contains”,但我想要得到的是下面的权重,它是权重的实际值:
<tbody>
<tr>
<th scope = "row">Weight</th>
<td> 26.7 g</td>
<tr>
<body>我要找的是获取文本26.7 g。我试过使用下面的方法,但似乎不起作用:
//text()[contains(.,'Weight')]//td有什么建议吗?提前谢谢。
发布于 2020-10-30 20:20:15
您可以使用following-sibling::td
from lxml import etree
txt = '''<tbody>
<tr>
<th scope = "row">Weight</th>
<td> 26.7 g</td>
</tr>
</tbody>'''
root = etree.fromstring(txt)
for td in root.xpath('//th[contains(., "Weight")]/following-sibling::td'):
print(td.text)打印:
26.7 ghttps://stackoverflow.com/questions/64608159
复制相似问题