【发布时间】:2019-03-05 00:24:40
【问题描述】:
我在一个对象中有以下 html 代码:
<span itemprop="price" content="187">187,00&nbsp;€</span>
我的想法是获取 span 对象的内容(价格)。为此,我正在执行以下操作:
import requests
from lxml import html
tree = html.fromstring(res.content)
prices = tree.xpath('//span[@class="price"]/text()')
print(float(prices[0].split()[0].replace(',','.')))
这里,res.content 包含在上面显示的 span 对象中。如您所见,我从187,00&nbsp;€(经过一些修改)获得价格,而从跨度内的“内容”标签中获取价格会更容易。我试过使用:
tree.xpath('//span[@class="price"]/content()')
但它不起作用。有没有办法检索这些数据?我愿意使用任何其他库。
【问题讨论】:
标签: python html web-scraping