scrapy发起payload形式的post请求

headers={

'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36',

'Referer':Referer,

'Content-Type':'application/json;charset=UTF-8',

'5c4d8797ab7576ce2339':'299fa9ae437bcd55403d6f6ef4d06a7a9270cc0b28c9b60a77f52ddc2b19b583c8f4215cc385ef7c4f3fef6097d91dc723f89d3c4f9b9768b2685a190c41400a'} 

 payload={"area":{"province":"31"},"key":brand,"page":i}

yieldscrapy.Request(url=url,  method='POST',  body=json.dumps(payload),  headers=headers,  cookies=random.choice(allcookie), meta={'brand':brand,'url':url},  callback=self.index, dont_filter=True)

###传递字符串格式的payload形式

url_rl = "https://www.yijiupi.com/v31/Product/ListProduct"

payload = '{"currentPage":1, 

"data":{"sonCategoryId":"%s","categoryIds":["%s"],"saleModel":-1,"sort":0,"currentPage":1,"pageSize":60}, 

"pageSize":60,

"addressId":1745761,

"deviceType":3

}'%(cat_id,cat_id)

# return [scrapy.Request(url=url_rl,  body=json.dumps(payload),  method="POST",  callback=self.get_page_num)]

#这里的payload是字典类型,要换成字符串类型json.dumps(payload),之后的payload就是字符串类型,直接写payload

request = scrapy.Request(url=url_rl, body=payload, method="POST", callback=self.get_page_sum)

#return [scrapy.Request(url=url_rl, body=json.dumps(payload), method="POST", callback=self.get_page_num)]

yield request

这里的payload之前是字典类型,用return发送请求是需要字符串类型,所以要换成字符串类型json.dumps(payload),在用yield发送请求是需要的是字典类型,所以直接写payload就行,上面的是yield发送请求,所以body直接等于payload就可以了。

data_url = eval('{"orderby": "0","productTypeId": "%s","currentPage": "%s","pageSize": "12"}' %(id,i))

request = scrapy.FormRequest(url=url, formdata=data_url, callback=self.get_parse,dont_filter=True)

##yield scrapy.FormRequest(roomUrl, formdata=payload, callback=self.parseInfo, dont_filter=True)

你可能感兴趣的:(scrapy发起payload形式的post请求)