我正在尝试将表格数据提供给scrapy.FormRequest对象。 formdata是以下结构的字典:
{
"param1": [
{
"paramA": "valueA",
"paramB": "valueB"
}
]
}
等效于以下代码,在scrapy shell中运行:
from scrapy import FormRequest
url = 'www.example.com'
method_post = 'POST'
formdata = <the above dict>
fr = FormRequest(url=url, method=method_post, formdata=formdata)
fetch(fr)
,并且我得到以下错误:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/chhk/.local/share/virtualenvs/project/lib/python3.6/site-packages/scrapy/http/request/form.py", line 31, in __init__
querystr = _urlencode(items, self.encoding)
File "/Users/chhk/.local/share/virtualenvs/project/lib/python3.6/site-packages/scrapy/http/request/form.py", line 66, in _urlencode
for k, vs in seq
File "/Users/chhk/.local/share/virtualenvs/project/lib/python3.6/site-packages/scrapy/http/request/form.py", line 67, in <listcomp>
for v in (vs if is_listlike(vs) else [vs])]
File "/Users/chhk/.local/share/virtualenvs/project/lib/python3.6/site-packages/scrapy/utils/python.py", line 119, in to_bytes
'object, got %s' % type(text).__name__)
TypeError: to_bytes must receive a unicode, str or bytes object, got dict
我尝试了各种解决方案,包括整个字符串,带有各种转义符,以及为使其更易于接受而改变字典的方法,但是没有一种解决方案可以消除此错误(我得到400响应)。
我知道formdata以及我正在做的所有其他事情都是正确的,因为我已经成功地在curl中复制了它(formdata是通过-d formdata.txt
提供的)。
有没有一种方法可以避免FormRequest无法处理复杂的字典结构?还是我错过了什么?
答案 0 :(得分:2)
您可以尝试使用formdata
参数代替body
。示例:
FormRequest(url=url, method=method_post, body=json.dumps(formdata))