爬虫——headers中的神坑

版权声明:记事、留心、分享;如有侵权、违规情况等,请及时联系原创作者。 https://blog.csdn.net/qq_27297393/article/details/85339778

1、Content-Length:
        最好在headers中不要加这个字段,通过工具测试是好好的,一写入爬虫就报错:(failed 1 times): 400 Bad Request。一个下午找不到哪里错了,吐血。。。。。

2、"Content-Type": "application/x-www-form-urlencoded"
         这里就不得不提chrom(按F12)中的query string parameters 和 from data,正常的post参数对应query string parameters;当"Content-Type"被设置成"application/x-www-form-urlencoded"时候,表单的请求参数就会被隐藏,不会再url中显现出来,此时的参数就在from data中。
        以下是两种post请求方法:
a、FormRequest(url,callback=self.parse,formdata=formdata,headers={"Content-Type": "application/x-www-form-urlencoded"})
b、Request(url, method="POST", body=json.dumps(formdata), headers={'Content-Type': 'application/x-www-form-urlencoded'}, callback=self.parse)

猜你喜欢

转载自blog.csdn.net/qq_27297393/article/details/85339778