我是 scrapy 的新手,对 Python 有一点了解。
我想检索项目['评级']。评级采用字符串“评级为 4”的形式,但我只想要数字...我怎样才能获得它?
我对下面的解决方案进行了思考,但不知道它们是否有意义。但没有一个起作用。
> item_pub['rating'] = review.xpath('/html/body//*/div[@class="details"]/table[@class="detailtoptable"]/tbody/tr[1]/td/img/@alt').re(r'\d+') #to extract only the number since the result with extract() would be "rating is 4"
或
> item_pub['rating'] = review.xpath('/html/body//*/div[@class="details"]/table[@class="detailtoptable"]/tbody/tr[1]/td/img/@alt')[-1:].extract() #to extract only the number since the result with extract() would be "rating is 4"
非常感谢您的帮助,并对我的英语感到抱歉,我希望我的问题很清楚。
最佳答案
你的思维方式没问题,使用正则表达式。你只是有一个糟糕的 Xpath。
这里有一些提示:
- 不需要做
/html/body//
,你可以做//
- 无需使用
//*
选择所有元素,只需稍后选择单个元素即可。您可以继续并选择所需的元素://div
- 如果您使用浏览器找到此 xpath,则很可能实际上没有
tbody
元素,因为浏览器经常添加这些元素
尝试这样:
item_pub['rating'] = review.xpath('//div[@class="details"]/table[@class="detailtoptable"]/tr[1]/td/img/@alt').re_first(r'\d+')
关于python - scrapy选择器xpath提取匹配的正则表达式或切片字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29973752/