是否有一种方法来获得所有的标签,有一个类属性在python使用scrapy?



假设我有一个html页面,它有许多不同的属性,它们都有相同的类:

<a href = "www.example1.com" class = "example-class" Example Text 1 />
<a href = "www.example2.com" class = "example-class" Example Text 2 />
<a href = "www.example3.com" class = "example-class" Example Text 3 />

是否有可能获得所有具有类"example-class"的文本的所有文本?在本例中,示例文本1,示例文本2和示例文本3

感谢

是的,你可以这样做:

def parse(self, response):
texts = response.css('a.example-class::text').getall()
for text in texts:
print(text)

Css选择符

a.example-class::text将针对具有example-class::text类的<a>元素,在scrapy中是一个特殊的选择器,将提取这些选择器的文本内容。

然而,你写你的元素看起来像:

<a href = "www.example1.com" class = "example-class" Example Text 1 />

看起来有点奇怪,正常情况下链接应该是这样的:

<a href = "www.example1.com" class = "example-class">Example Text 1</a>

如果你写错了,上面的代码应该可以工作。

最新更新