【scrapy】FormRequest <TypeError: to_bytes must receive a unicode, str or bytes object, got int>
原因:
formdata的dict中,有value是int.......(有value不是byte类)
1 class FormRequest(Request): 2 3 def __init__(self, *args, **kwargs): 4 formdata = kwargs.pop('formdata', None) 5 if formdata and kwargs.get('method') is None: 6 kwargs['method'] = 'POST' 7 8 super(FormRequest, self).__init__(*args, **kwargs) 9 10 if formdata: 11 items = formdata.items() if isinstance(formdata, dict) else formdata 12 querystr = _urlencode(items, self.encoding)#就这句.... 13 if self.method == 'POST': 14 self.headers.setdefault(b'Content-Type', b'application/x-www-form-urlencoded') 15 self._set_body(querystr) 16 else: 17 self._set_url(self.url + ('&' if '?' in self.url else '?') + querystr)
def _urlencode(seq, enc): values = [(to_bytes(k, enc), to_bytes(v, enc)) for k, vs in seq for v in (vs if is_listlike(vs) else [vs])] return urlencode(values, doseq=1)
def to_bytes(text, encoding=None, errors='strict'): """Return the binary representation of `text`. If `text` is already a bytes object, return it as-is.""" if isinstance(text, bytes): #这里FALSE了,raise了TypeError return text if not isinstance(text, six.string_types): raise TypeError('to_bytes must receive a unicode, str or bytes ' 'object, got %s' % type(text).__name__) if encoding is None: encoding = 'utf-8' return text.encode(encoding, errors)