我正在尝试将 formdata 提供给一个 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
我尝试了多种解决方案,包括将整个内容作为字符串,使用各种转义字符以及 dict 的变体以使其更容易接受,但是消除此错误的解决方案都不适用于请求(我得到 400回复)。
我知道 formdata 以及我所做的一切都是正确的,因为我已经在 curl 中成功复制了它(formdata 是通过 提供的-d formdata.txt
)。
有没有办法解决 FormRequest 无法处理复杂的 dict 结构?还是我错过了什么?