使用带有 Scrapy 的 css 选择器获取 href [英] Get href using css selector with Scrapy
本文介绍了使用带有 Scrapy 的 css 选择器获取 href的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获取href
值:
<span class="title">
<a href="https://www.example.com"></a>
</span>
我试过了:
Link = Link1.css('span[class=title] a::text').extract()[0]
But I just get the text inside the <a>
. How can I get the link inside the href
?
推荐答案
您正在寻找的是:
Link = Link1.css('span[class=title] a::attr(href)').extract()[0]
因为你也匹配了一个 span
class" 属性,你甚至可以写
Since you're matching a span
"class" attribute also, you can even write
Link = Link1.css('span.title a::attr(href)').extract()[0]
请注意::text
伪元素和::attr(attributename)
功能伪元素不是 标准的CSS3 选择器.它们是 Scrapy 0.20 中 CSS 选择器的扩展.
Please note that ::text
pseudo element and ::attr(attributename)
functional pseudo element are NOT standard CSS3 selectors. They're extensions to CSS selectors in Scrapy 0.20.
Edit (2017-07-20): 从 Scrapy 1.0 开始,你可以使用 .extract_first()
而不是 .extract()[0]
Edit (2017-07-20): starting from Scrapy 1.0, you can use .extract_first()
instead of .extract()[0]
Link = Link1.css('span[class=title] a::attr(href)').extract_first()
Link = Link1.css('span.title a::attr(href)').extract_first()
这篇关于使用带有 Scrapy 的 css 选择器获取 href的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文