gpt4 book ai didi

python - lxml 中的 POST 方法表单使用 submit_form 引发 TypeError

转载 作者:太空狗 更新时间:2023-10-29 19:29:43 24 4
gpt4 key购买 nike

我正在尝试使用 lxml 提交 POST 方法表单,但出现类型错误。这是引发此错误的最小示例:

>>> 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 内部生成的,就像这样。有谁知道这是错误还是预期行为以及如何解决它?

最佳答案

来自 https://github.com/lxml/lxml/pull/122/files :

“在 python3 中,urlopen 需要 POST 数据的字节流。此补丁在传输前将数据编码为 utf-8。”在 src/lxml/html/__init__.py 中,更改第 918 行,

data = urlencode(values)

data = urlencode(values).encode('utf-8')

关于python - lxml 中的 POST 方法表单使用 submit_form 引发 TypeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13151333/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com