我正在尝试抓取亚马逊,但我获得的file.csv是空白的。看看我的代码:
# -*- coding: utf-8 -*-
import scrapy
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
from scrapy.exceptions import CloseSpider
from mercado.items import MercadoItem
class MercadoSpider(CrawlSpider):
name = 'mercado'
item_count = 0
allowed_domain = ['www.amazon.es']
start_urls = ['https://www.amazon.es/s/ref=sr_st_price-asc-rank?keywords=febi+bilstein&rh=i%3Aaps%2Ck%3Afebi+bilstein&__mk_es_ES=%C3%85M%C3%85Z%C3%95%C3%91&qid=1521977786&sort=price-asc-rank']
rules = {
Rule(LinkExtractor(allow =(), restrict_xpaths = ('//*[@id="pagnNextString"]'))),
Rule(LinkExtractor(allow =(), restrict_xpaths = ('//h2')),
callback = 'parse_item', follow = False)
}
def parse_item(self, response):
ml_item = MercadoItem()
#info de producto
ml_item['articulo'] = response.xpath('//*[@id="result_0"]/div/div/div/div[2]/div[1]/div[1]/a/h2').extract()
ml_item['precio'] = response.xpath('//*[@id="result_0"]/div/div/div/div[2]/div[2]/div[1]/div[1]/a/span[2]').extract()
self.item_count += 1
if self.item_count > 10:
raise CloseSpider('item_exceeded')
yield ml_item
我不知道为什么我得不到结果。你能帮我个忙吗?
发布于 2018-03-26 01:13:27
您的第二个LinkExtractor
尝试提取h2
元素中的所有链接。
要匹配所有包含h2
元素的链接,可以使用像//a[h2]
这样的xpath
修复之后,您将遇到parse_item
中的xpath与任何内容都不匹配的问题,因此您也需要修复这些问题。
此外,为了在抓取一定数量的项目后关闭爬行器,还有CLOSESPIDER_ITEMCOUNT设置。
https://stackoverflow.com/questions/49475968
复制相似问题