我是 scrapy 的新手,我正在尝试抓取宜家网站网页。包含给定位置列表的基本页面 here .
我的 items.py 文件如下:
import scrapy
class IkeaItem(scrapy.Item):
name = scrapy.Field()
link = scrapy.Field()
下面给出了蜘蛛:
import scrapy
from ikea.items import IkeaItem
class IkeaSpider(scrapy.Spider):
name = 'ikea'
allowed_domains = ['http://www.ikea.com/']
start_urls = ['http://www.ikea.com/']
def parse(self, response):
for sel in response.xpath('//tr/td/a'):
item = IkeaItem()
item['name'] = sel.xpath('a/text()').extract()
item['link'] = sel.xpath('a/@href').extract()
yield item
运行文件时我没有得到任何输出。 json 文件输出类似于:
[[{"link": [], "name": []}
我要查找的输出是位置名称和链接。我一无所获。 我哪里错了?
最佳答案
项目字段的 xpath 表达式中存在一个简单的错误。循环已经遍历 a
标签,您不需要在内部 xpath 表达式中指定 a
。换句话说,当前您正在 tr
内的 td
内的 a
标签内搜索 a
标签。这显然没有结果。
将a/text()
替换为text()
,将a/@href
替换为@href
。
(测试 - 适合我)
关于python - Scrapy:提取链接和文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27753232/