当前位置: 首页>编程语言>正文

Scrapy-scrapy.FormRequest方法的小坑

使用scrapy.FromRequest()方法时,如果formdata设置为{}即没有post body,则会变为get请求,只有当不为空字典时才会变为POST请求,因此无post数据时,应该使用scrapy.Request(url,method='POST')

原因:查看FromRequest源码,部分如下

class FormRequest(Request):
    valid_form_methods = ['GET', 'POST']

    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
            querystr = _urlencode(items, self.encoding)
            if self.method == 'POST':
                self.headers.setdefault(b'Content-Type', b'application/x-www-form-urlencoded')
                self._set_body(querystr)
            else:
                self._set_url(self.url + ('&' if '?' in self.url else '?') + querystr)

可以看到formdata为空字典时,第一个判断为假,随后调用父类Request的__init__方法,部分源码如下

class Request(object_ref):

    def __init__(self, url, callback=None, method='GET', headers=None, body=None,
                 cookies=None, meta=None, encoding='utf-8', priority=0,
                 dont_filter=False, errback=None, flags=None, cb_kwargs=None):

        self._encoding = encoding  # this one has to be set first
        self.method = str(method).upper()
        self._set_url(url)
        self._set_body(body)
        assert isinstance(priority, int), "Request priority not an integer: %r" % priority
        self.priority = priority

method参数默认值为‘GET’,因此当formdata设置为{}会变为get请求。


https://www.xamrdz.com/lan/57c1963915.html

相关文章: