我开始在一个小项目中使用Scrapy,但我无法提取链接。每次找到类时,我只得到"[]“,而不是url。我是不是漏掉了什么明显的东西?
sel = Selector(response)
for entry in sel.xpath("//div[@class='recipe-description']"):
print entry.xpath('href').extract()来自网站的示例:
<div class="recipe-description">
<a href="http://www.url.com/">
<h2 class="rows-2"><span>SomeText</span></h2>
</a>
</div>发布于 2016-03-29 19:06:14
您的xpath查询错误
for entry in sel.xpath("//div[@class='recipe-description']"):在这一行中,您实际上是在迭代没有任何Href属性的div
为了使其正确,您应该在div中选择achor元素
for entry in sel.xpath("//div[@class='recipe-description']/a"):
print entry.xpath('href').extract()最好的解决方案是直接在for循环中提取href属性
for href in sel.xpath("//div[@class='recipe-description']/a/@href").extract():
print href为简单起见,您还可以使用css选择器
for href in sel.css("div.recipe-description a::attr(href)").extract():
print hrefhttps://stackoverflow.com/questions/36281413
复制相似问题