10

我正在尝试使用 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 内部生成。有谁知道这是一个错误,还是预期的行为以及如何解决它?

4

3 回答 3

1

来自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')
于 2014-02-21T21:58:51.187 回答
0

它是 Python 3,所以你应该写

form.fields['your_name'] = b'Morphit'

或者

form.fields['your_name'] = 'Morphit'.encode('utf-8')
于 2013-12-14T22:03:47.510 回答
0
def myopen_http(method, url, values):
   if not url:
      raise ValueError("cannot submit, no URL provided")
   ## FIXME: should test that it's not a relative URL or something
   try:
      from urllib import urlencode, urlopen
   except ImportError: # Python 3
      from urllib.request import urlopen
      from urllib.parse import urlencode
   if method == 'GET':
      if '?' in url:
         url += '&'
      else:
         url += '?'
         url += urlencode(values)
         data = None
   else:
      data = urlencode(values).encode('utf-8')

   return urlopen(url, data)

result = lxml.html.parse(lxml.html.submit_form(form, open_http=myopen_http))
于 2015-01-25T08:51:41.183 回答