Python 搔痒亚马逊蜘蛛
我试图在亚马逊上搜索,但是我得到的file.csv是空白的。 看看我的代码:Python 搔痒亚马逊蜘蛛,python,web-scraping,scrapy,scrapy-spider,Python,Web Scraping,Scrapy,Scrapy Spider,我试图在亚马逊上搜索,但是我得到的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
# -*- 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
我不知道为什么我得不到结果。
您能帮助我吗?您的第二个
链接提取程序尝试提取h2
元素中的所有链接。
要匹配包含h2
元素的所有链接,可以使用类似于//a[h2]
修复后,您将遇到parse\u item
中的XPath不匹配的问题,因此您也需要修复这些问题
此外,对于在刮取一定数量的物品后关闭爬行器,还有一个设置。非常感谢!你是对的。现在它起作用了。我现在的问题是“503服务不可用”,所以我被阻止了。你知道我如何在代码中添加“用户代理”来避免这种情况吗?