0

我正在尝试使用 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'
4

1 回答 1

0

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

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

于 2013-06-22T01:11:56.203 回答