我有一个问题,我刮一个子页面与链接,我在主页上获得。你知道吗
每个漫画都有自己的页面,所以我试着打开每一个项目的页面,并刮价。你知道吗
这是蜘蛛:
class PaniniSpider(scrapy.Spider):
name = "spiderP"
start_urls = ["http://comics.panini.it/store/pub_ita_it/magazines.html"]
def parse(self, response):
# Get all the <a> tags
for sel in response.xpath("//div[@class='list-group']//h3/a"):
l = ItemLoader(item=ComicscraperItem(), selector=sel)
l.add_xpath('title', './text()')
l.add_xpath('link', './@href')
request = scrapy.Request(sel.xpath('./@href').extract_first(), callback=self.parse_isbn, dont_filter=True)
request.meta['l'] = l
yield request
def parse_isbn(self, response):
l = response.meta['l']
l.add_xpath('price', "//p[@class='special-price']//span/text()")
return l.load_item()
问题是关于链接,输出类似于:
{"title": "Spider-Man 14", "link": ["http://comics.panini.it/store/pub_ita_it/mmmsm014isbn-it-marvel-masterworks-spider-man-marvel-masterworks-spider.html"], "price": ["\n \u20ac\u00a022,50 ", "\n \u20ac\u00a076,50 ", "\n \u20ac\u00a022,50 ", "\n \u20ac\u00a022,50 ", "\n \u20ac\u00a022,50 ", "\n \u20ac\u00a018,00
{"title": "Avenger di John Byrne", "link": ["http://comics.panini.it/store/pub_ita_it/momae005isbn-it-omnibus-avengers-epic-collecti-marvel-omnibus-avengers-by.html"], "price": ["\n \u20ac\u00a022,50 ", "\n \u20ac\u00a076,50 ", "\n \u20ac\u00a022,50
简而言之,请求传递每个项目的链接列表,因此价格不是唯一的,而是列表的结果。你知道吗
如何只传递相关项目的链接并存储每个项目的价格?你知道吗
我看到两种方法:
使用
response.xpath
在子页面中获取它或者在主页上得到所有需要的信息-标题,链接和价格的div
然后就不必使用
parse_isbn
对于测试,我使用了独立脚本,您可以将它放在一个文件中运行,而无需创建项目。你知道吗
它正确地得到了价格。你知道吗
编辑:如果必须加载其他页面,则可以将
add_value
与response.xpath().get()
一起使用,而不是add_xpath
完整示例:
通过继承scrapy的项目加载器并应用默认的\u output\u processor=TakeFirst()来创建项目加载器
例如
你也可以参考我的项目https://github.com/yashpokar/amazon-crawler/blob/master/amazon/loaders.py
相关问题 更多 >
编程相关推荐