转换为Python Scrapy请求时,Python request.post()不起作用
我有简单的POST请求代码转换为Python Scrapy请求时,Python request.post()不起作用,python,scrapy,python-requests,scrapy-spider,Python,Scrapy,Python Requests,Scrapy Spider,我有简单的POST请求代码 headers = { dictionary of headers } params = ( ('param1', '0'), ('param2', '5668294380'), ('param3', '8347915011'), ) response = requests.post('https://website.com', headers=headers, params=params, data=__data) 这可以作为独
headers = {
dictionary of headers
}
params = (
('param1', '0'),
('param2', '5668294380'),
('param3', '8347915011'),
)
response = requests.post('https://website.com', headers=headers, params=params, data=__data)
这可以作为独立的Python程序完美地工作
但是我想用Python Scrapy来做这个
Request(url='https://website.com',callback=self.callback_fun, headers=headers, body=__data, method="POST")
它给了我一个URL无法处理POST请求的响应
我试过了
FormRequest(url='https://website.com',callback=self.callback_fun, headers=headers, body=__data)
Request(url='https://website.com?' + urllib.urlencode(self.params),callback=self.callback_fun, headers=headers, body=__data, method="POST")
它给了我同样的回答
我试过了
FormRequest(url='https://website.com',callback=self.callback_fun, headers=headers, body=__data)
Request(url='https://website.com?' + urllib.urlencode(self.params),callback=self.callback_fun, headers=headers, body=__data, method="POST")
但是它给了我400个错误的请求
刮痧怎么了?我的意思是纯Python脚本可以工作,但在Scrapy中不起作用
我认为主要问题是如何使用Scrapy发送params=params
。Scrapy仅允许通过body
参数发送请求有效负载
class scrapy.http.FormRequest(url[, formdata, ...])
参数:formdata(元组的dict或iterable)–是一个字典
(或(键、值)元组的一部分)包含HTML表单数据
将进行url编码并分配给请求主体
在HTTP中,如果要发布数据,数据将在请求正文中设置并编码。您可以自己对dict进行编码,也可以使用ScrapyFormRequest
:
class FormRequest(Request):
def __init__(self, *args, **kwargs):
formdata = kwargs.pop('formdata', None)
if formdata and kwargs.get('method') is None:
kwargs['method'] = 'POST'
super(FormRequest, self).__init__(*args, **kwargs)
if formdata:
items = formdata.items() if isinstance(formdata, dict) else formdata
# encode dict here
querystr = _urlencode(items, self.encoding)
if self.method == 'POST':
# set message header
self.headers.setdefault(b'Content-Type', b'application/x-www-form-urlencoded')
# set message body
self._set_body(querystr)
else:
self._set_url(self.url + ('&' if '?' in self.url else '?') + querystr)
----------------------------更新--------------
在请求代码中:
response = requests.post('https://website.com', headers=headers, params=params, data=__data)
它首先将参数添加到URL,然后将post数据添加到修改后的URL。您应该更改您的URL。您可以通过以下方式获取URL:
print(response.url)
通过尝试这两种方法而不是在实际的网站上检查实际发送的请求。可能有一些标题被刮擦覆盖了。@AshishNitinPatil我从Chrome开发工具中复制了这个URL作为cURL。该cURL在Shell/Terminal中工作得非常好,我还使用该工具将该cURL转换为Python请求POST。而且Python代码也能工作!。只有在Scrapy中它不起作用它不是URL,我要求您在httpbin.org上查看它并比较您得到的响应。它就像一面镜子,所以你知道你从不同方法发出的两个请求有什么不同。我仍然得到
HTTP方法POST不受此URL支持https://website,callback=self.parse_response,headers=self.headers,formdata=self.params,body=u data)
对不起,我没听清楚。。。你想让我更改URL吗?还是怎样还有,我应该在哪里放置代码类FormRequest
???在Spider file?@Umair-in-yourequests
code中,打印response.url
以检查数据发布的位置。这是在Scrapy中需要使用的真实url。response.url
ishttps://website.com/ko/website/g?authuser=0&__u=5668294380&__c=8347915011
但是当使用FormRequest执行此操作时
Scrapy打印`调试:重定向(301)到从`DEBUG:Crawled(405)(参考:https://website.com/ko/website/Home?__u=5668294380&__c=8347915011&authuser=0)