首先Scrapy 发送payload请求格式如下:
def start_requests(self): querystr = { "ctoken": "U-ang1zmpP6c3VO4", "sceneKey": "DEFAULT", "pdKey": "P_ECTBILL_QUOTATION1", } payload = { "executeKey": "E_QUOTATION-RATE_QUERY_NEW", "pdKey": "P_ECTBILL_QUOTATION1", "requestJson": '[{"acceptBankNo":"313143005157","bankLevel":"04","beginReceiptMoney":"0","endReceiptMoney":"500000","billReceiptMoney":"20000","expireDay":1577783940394,"billBizType":"KHT"}]', "sceneKey": "DEFAULT", } data = json.dumps(payload) # payload格式需要用dumps转成string api = self.url + urlencode(querystr) # Query string parameter yield Request(url=api, method='POST', body=data, callback=self.parse, headers=self.headers)
报错的原因是因为header中有带参数 HOST, Content-Length, 屏蔽掉这两个参数就行了,最好是养成习惯header中都不要带这两个参数 .
原因可参考:https://stackoverflow.com/questions/42248903/scrapy-post-request-not-working-400-bad-request