在scrapy中提取类名 [英] Extract class name in scrapy
本文介绍了在scrapy中提取类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从 trustpilot.com 中删除评级.
是否可以使用scrapy提取类名?我正在尝试抓取由五个单独图像组成的评级,但这些图像位于一个带有评级名称的类中,例如,如果评级为 2 则开始:
...如果是 3 星,则:
...那么有没有办法我可以刮掉类 count-2
或 count-3
假设选择器像 .css('.star-rating')
?
解决方案 您可以在代码中的某处结合使用两者:
导入重新classes = response.css('.star-rating').xpath("@class").extract()对于类中的 cls:match = re.search(r'count-d+', cls)如果匹配:print("Class = {}".format(match.group(0))
I am trying to scrape rating off of trustpilot.com.
Is it possible to extract a class name using scrapy? I am trying to scrape a rating which is made up of five individual images but the images are in a class with the name of the rating for example if the rating is 2 starts then:
<div class="star-rating count-2 size-medium clearfix">...
if it is 3 stars then:
<div class="star-rating count-3 size-medium clearfix">...
So is there a way I can scrape the class count-2
or count-3
assuming a selector like .css('.star-rating')
?
解决方案 You could use a combination of both somewhere in your code:
import re
classes = response.css('.star-rating').xpath("@class").extract()
for cls in classes:
match = re.search(r'count-d+', cls)
if match:
print("Class = {}".format(match.group(0))
这篇关于在scrapy中提取类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文
相关文章
- 提取scrapy中的类名;
- 在 Scrapy 中提取图像;
- Scrapy 如何在属性内部的类中提取文本?;
- Scrapy - 从表中提取项目;
- Scrapy 从 div 中提取文本;
- 从平面 Modelica 代码中提取类型/类名;
- 如何在scrapy中提取javascript值;
- Scrapy:从 HTML 脚本中提取 JSON;
- Scrapy:从HTML脚本中提取JSON;
- 使用 Python/Scrapy 在 h1 中提取 p;
- 如何从CSS选择器中提取类名?;
- Scrapy:从源及其链接中提取数据;
- 使用 Scrapy 从动态 JSON 响应中提取;
- 如何使用Scrapy从JavaScript中提取jsonObj;
- 使用scrapy从gsmarena页面中提取数据;
- 在 Bash 中提取文件名和扩展名;
- 用Python / Scrapy在h1中提取p;
- Scrapy 和 Xpath 从 javascript 代码中提取数据;
- 从类中提取接口;
- 从文件名中提取号码;
- 从路径中提取文件名;
- 如何从矩阵中提取行名?;
- 从路径中提取文件名;
- 从文件名中提取日期;
- 使用scrapy从200k域中提取文本;
Python最新文章
- 类型错误:只有长度为1的阵列可以尝试拟合指数的数据转换到Python标量;
- bs4.FeatureNotFound:找不到一棵树建设者您所要求的功能:LXML。你需要安装一个解析器库?;
- 系列的真值是不明确的。使用a.empty,a.bool(),a.item(),a.any()或a.all();
- (unicode错误)'unicodeescape'编解码器无法解码位置2-3中的字节:truncated \UXXXXXXXX escape;
- 将pandas dataframe中的列从int转换为string;
- Python:由实例对象调用方法:“missing 1 required positional argument:'self'”;
- Sparksql过滤与多个条件(与where子句中选择);
- JSONDe codeError:期待值:1行1列(CHAR 0);
- Cmake不能找到Python库;
- Python - 将Dataframe中的所有项目转换为字符串;