假设我有一个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>
如果你写错了,上面的代码应该可以工作。