如何使用xpath()在<span>元素中获取值?

2024-04-19 12:22:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用scrapy python在元素中获得货币值510940:

<span class="price-amount">
    <span class="currency_symbol">₫</span>
    510,940
</span>

我的密码是:

item["price"] = response.xpath("//span[@class='price-amount']/text()").extract()

Tags: text元素密码response货币extractsymbolitem
2条回答

以下是pricecurrency的代码:

>>> txt = """<span class="price-amount">
...     <span class="currency_symbol">₫</span>
...     510,940
... </span>"""
>>> sel = Selector(text=txt)
>>> sel.xpath('//span[@class="price-amount"]/span[@class="currency_symbol"]/following-sibling::text()').get()
u'\n    510,940\n'
>>> sel.xpath('//span[@class="price-amount"]/span[@class="currency_symbol"]/text()').get()
u'\u20ab'

用你的代码,我得到['\n ', '\n 510,940\n']。你知道吗

如果需要510,940,可以使用:

  • re:test(., '\d')筛选不包含数字的字符串

  • .get()(或者.extract_first()如果你想走老路的话)将单个项目提取为字符串,而不是匹配字符串的列表。

  • .strip()删除周围类似空格的字符。

即:

response.xpath("//span[@class='price-amount']/text()[re:test(., '\d')]").get().strip()

另外,对于价格提取,您可能需要使用专门的库,例如price-parser。你知道吗

相关问题 更多 >