scrapy post请求formdata与body的区别

1、Request的post请求,body是字符串

page = 1
keyword = "螺丝"
req_body = '{"keyword":"%s","page":"%s"}' % (keyword, page)
# req_body = {"keyword":keyword,"page":page}
self.gaj_headers['Referer'] = 'https://m.mymro.cn/pages/product/list?keyword=%s'%(parse.quote(keyword))
yield scrapy.Request(url=self.gaj_url, method="POST", body=req_body,  headers=self.gaj_headers,
                     callback=self.get_gaj_goods_list,dont_filter=False)

2、FormRequest请求,formdata是字典,且字典中的值必须是字符串,例如下面这样ym_data [‘cateid’]= ‘0’,而不能是ym_data [‘cateid’]=0;

self.ym_data = {
    'manuId': '0',
    'attributes': '',
    'order': '0',
    'cateid': '0',
    'cateid': 0,
    'pageIndex': str(self.currentPage),
}
keyword = "螺丝"
elf.ym_data = {
     'keyword': keyword,
 }
yield scrapy.FormRequest(url=self.ym_url, formdata=self.ym_data,
                          callback=self.get_ym_detail, dont_filter=True)

你可能感兴趣的:(python,scrapy,python)