Tro*_*rot 4 python scrapy web-scraping
我开始将Scrapy用于一个小项目,但我无法提取链接.每次找到班级时,我只获得"[]"而不是网址.我错过了一些明显的东西吗
sel = Selector(response)
for entry in sel.xpath("//div[@class='recipe-description']"):
print entry.xpath('href').extract()
Run Code Online (Sandbox Code Playgroud)
来自网站的样本:
<div class="recipe-description">
<a href="http://www.url.com/">
<h2 class="rows-2"><span>SomeText</span></h2>
</a>
</div>
Run Code Online (Sandbox Code Playgroud)
akh*_*hab 15
你的xpath查询是错误的
for entry in sel.xpath("//div[@class='recipe-description']"):
Run Code Online (Sandbox Code Playgroud)
在这一行中,您实际上正在迭代我们没有任何Href属性的div
为了使其正确,您应该选择以下achor元素div:
for entry in sel.xpath("//div[@class='recipe-description']/a"):
print entry.xpath('href').extract()
Run Code Online (Sandbox Code Playgroud)
最好的解决方案是直接href在for循环中提取属性
for href in sel.xpath("//div[@class='recipe-description']/a/@href").extract():
print href
Run Code Online (Sandbox Code Playgroud)
为简单起见,您还可以使用css选择器
for href in sel.css("div.recipe-description a::attr(href)").extract():
print href
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10177 次 |
| 最近记录: |