我正在尝试使用 lxml 提交 POST 方法表单,但我收到了 TypeError。这是引发此错误的最小示例:
>>> import lxml.html
>>> page = lxml.html.parse("http://www.webcom.com/html/tutor/forms/start.shtml")
>>> form = page.getroot().forms[0]
>>> form.fields['your_name'] = 'Morphit'
>>> result = lxml.html.parse(lxml.html.submit_form(form))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.3/site-packages/lxml/html/__init__.py", line 887, in submit_form
return open_http(form.method, url, values)
File "/usr/lib/python3.3/site-packages/lxml/html/__init__.py", line 907, in open_http_urllib
return urlopen(url, data)
File "/usr/lib/python3.3/urllib/request.py", line 160, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python3.3/urllib/request.py", line 471, in open
req = meth(req)
File "/usr/lib/python3.3/urllib/request.py", line 1183, in do_request_
raise TypeError(msg)
TypeError: POST data should be bytes or an iterable of bytes. It cannot be of type str.
我在网上其他地方找到了确切的错误,但我没有看到它像这样从 lxml 内部生成。有谁知道这是一个错误,还是预期的行为以及如何解决它?