2024-04-19 12:22:55 发布
网友
我使用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()
以下是price和currency的代码:
price
currency
>>> 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']。你知道吗
['\n ', '\n 510,940\n']
如果需要510,940,可以使用:
510,940
re:test(., '\d')筛选不包含数字的字符串
re:test(., '\d')
.get()(或者.extract_first()如果你想走老路的话)将单个项目提取为字符串,而不是匹配字符串的列表。
.get()
.extract_first()
.strip()删除周围类似空格的字符。
.strip()
即:
response.xpath("//span[@class='price-amount']/text()[re:test(., '\d')]").get().strip()
另外,对于价格提取,您可能需要使用专门的库,例如price-parser。你知道吗
以下是
price
和currency
的代码:用你的代码,我得到
['\n ', '\n 510,940\n']
。你知道吗如果需要
510,940
,可以使用:re:test(., '\d')
筛选不包含数字的字符串.get()
(或者.extract_first()
如果你想走老路的话)将单个项目提取为字符串,而不是匹配字符串的列表。.strip()
删除周围类似空格的字符。即:
另外,对于价格提取,您可能需要使用专门的库,例如price-parser。你知道吗
相关问题 更多 >
编程相关推荐