<unknown get =“”form =“”encoding =“”type =“”'multipart =“”form-data'=“”>使用Python的机械化</unknown>

时间:2013-06-22 00:46:01

标签: python mechanize

我正在尝试使用机械化将图片的URL提交到网站的图像搜索引擎。这是我的代码位。我选择的图像是从互联网中随机选择的。

r = br.open('http://stu.baidu.com/')
for f in br.forms():
    print f
br.select_form(nr=1)

br.form['objurl']='http://images2.fanpop.com/image/photos/13900000/Pretty-Dogs-in-Garden-dogs-13905929-1920-1200.jpg'
br.submit()
print br.submit

然而,这是我从终端响应得到的。我不知道我错在哪里....

face2.py:13: UserWarning: gzip transfer encoding is experimental!
  br.set_handle_gzip(True)
<form2 POST http://stu.baidu.com/i?rt=0&rn=10&stt=0&ct=0&tn=baiduimage multipart/form-data
  <FileControl(image=<No files added>)>>
<form1 GET http://stu.baidu.com/i multipart/form-data
  <TextControl(objurl=)>
  <SubmitControl(<None>=识图一下) (readonly)>
  <HiddenControl(filename=) (readonly)>
  <HiddenControl(rt=0) (readonly)>
  <HiddenControl(rn=10) (readonly)>
  <HiddenControl(ftn=indexstu) (readonly)>
  <HiddenControl(ct=1) (readonly)>
  <HiddenControl(stt=0) (readonly)>
  <HiddenControl(tn=shituresult) (readonly)>>
Traceback (most recent call last):
  File "face2.py", line 42, in <module>
    br.submit()
  File "/Library/Python/2.7/site-packages/mechanize-0.2.5-py2.7.egg/mechanize/_mechanize.py", line 541, in submit
    return self.open(self.click(*args, **kwds))
  File "/Library/Python/2.7/site-packages/mechanize-0.2.5-py2.7.egg/mechanize/_mechanize.py", line 530, in click
    request = self.form.click(*args, **kwds)
  File "/Library/Python/2.7/site-packages/mechanize-0.2.5-py2.7.egg/mechanize/_form.py", line 2999, in click
    self._request_class)
  File "/Library/Python/2.7/site-packages/mechanize-0.2.5-py2.7.egg/mechanize/_form.py", line 3201, in _click
    return control._click(self, coord, return_type, request_class)
  File "/Library/Python/2.7/site-packages/mechanize-0.2.5-py2.7.egg/mechanize/_form.py", line 2350, in _click
    r = form._switch_click(return_type, request_class)
  File "/Library/Python/2.7/site-packages/mechanize-0.2.5-py2.7.egg/mechanize/_form.py", line 3269, in _switch_click
    req_data = self._request_data()
  File "/Library/Python/2.7/site-packages/mechanize-0.2.5-py2.7.egg/mechanize/_form.py", line 3235, in _request_data
    "unknown GET form encoding type '%s'" % self.enctype)
ValueError: unknown GET form encoding type 'multipart/form-data'

1 个答案:

答案 0 :(得分:0)

我似乎无法加载百度,但它可能与“UserWarning:gzip传输编码是实验性的”有关。或者,您可能需要urllib.urlencode您的GET数据。

一些例子: http://pymotw.com/2/urllib/ http://kentsjohnson.com/kk/00010.html