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

时间:2012-10-31 04:35:08

标签: python python-3.x lxml

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

3 个答案:

答案 0 :(得分: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')

答案 1 :(得分:0)

这是Python 3,所以你应该写

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

form.fields['your_name'] = 'Morphit'.encode('utf-8')

答案 2 :(得分: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))