Python 在Scrapy请求中持久化cookie 我想刮什么?
事实上,我正在努力清理一个产品网站,为每个杂志收集一些产品信息。为此,我使用必要的POST请求指定我的magasin(获取相应的cookie),然后对我的类别执行get。Scrapy已经建立了一种机制来发送带有cookie的请求。我的问题是,在某些时候,Python 在Scrapy请求中持久化cookie 我想刮什么?,python,web-scraping,scrapy,Python,Web Scraping,Scrapy,事实上,我正在努力清理一个产品网站,为每个杂志收集一些产品信息。为此,我使用必要的POST请求指定我的magasin(获取相应的cookie),然后对我的类别执行get。Scrapy已经建立了一种机制来发送带有cookie的请求。我的问题是,在某些时候,parse中的请求是用相同的cookie发出的,这不是我想要的 parse_mag我创建的只是检查我是否在特定的magasin中 .您的parse()方法总是对同一url发出完全相同的请求,并且parse\u mag()会随响应一起被调用 因此,
parse
中的请求是用相同的cookie发出的,这不是我想要的
parse_mag
我创建的只是检查我是否在特定的magasin中
.您的parse()
方法总是对同一url发出完全相同的请求,并且parse\u mag()
会随响应一起被调用
因此,并不是对单个POST请求多次调用parse_mag()
,而是对每个请求调用一次,使用相同的参数,返回相同的结果
使用
class BricoMarcheSpider(scrapy.Spider):
name = 'brico_marche'
def start_requests(self):
# full path
with open('file.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
# check empty value
magasin_id = row['Id']
if row['Id'][0] == '0':
magasin_id = row['Id'][1:]
formdata = {'city' : row['City'], 'market' : row['Brand'], 'idPdv' : magasin_id}
#print(row['City'], row['Brand'], row['Id'])
yield scrapy.FormRequest(url='http://www.bricomarche.com/bma_popin/Geolocalisation/choisirMagasin', formdata=formdata, dont_filter=True, callback=self.parse)
def parse(self, response):
yield scrapy.Request('http://www.bricomarche.com/l/nos-produits/jardin/abri-garage-carport-et-rangement/abri-de-jardin/les-abris-bois-1121.html?limit=90', dont_filter=True, callback=self.parse_mag)
def parse_mag(self, response):
yield {"City" : response.xpath('//div[@class="store-details"]/p/strong/text()').extract_first()}
def start_requests(self):
# full path
with open('file.csv') as csvfile:
reader = csv.DictReader(csvfile)
for i, row in enumerate(reader):
# check empty value
magasin_id = row['Id']
if row['Id'][0] == '0':
magasin_id = row['Id'][1:]
formdata = {'city' : row['City'], 'market' : row['Brand'], 'idPdv' : magasin_id}
#print(row['City'], row['Brand'], row['Id'])
yield scrapy.FormRequest(url='http://www.bricomarche.com/bma_popin/Geolocalisation/choisirMagasin', formdata=formdata, dont_filter=True, callback=self.parse, meta={'cookiejar': i})
def parse(self, response):
yield scrapy.Request('http://www.bricomarche.com/l/nos-produits/jardin/abri-garage-carport-et-rangement/abri-de-jardin/les-abris-bois-1121.html?limit=90', dont_filter=True, callback=self.parse_mag, meta={'cookiejar': response.meta['cookiejar']})