我开始在一个小项目中使用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>
你的 xpath 查询是错误的
for entry in sel.xpath("//div[@class='recipe-description']"):
在这一行中,您实际上是在迭代我们没有任何 Href 属性的 div
为了使其正确,您应该选择achor
中的元素div
:
for entry in sel.xpath("//div[@class='recipe-description']/a"):
print entry.xpath('href').extract()
最好的解决方案是提取href
属性在for
直接循环
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 href
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)