2

到目前为止,我有执行此操作的机械化代码:

goes to a site
logs in
submits a form

这是我遇到问题的地方。我需要它做的是将响应(文件)写入本地文件。就 python 与文件系统交互而言,我一无所知。

提前致谢

编辑:这是我目前拥有的一些代码

br = mechanize.Browser()
br.set_handle_robots(False)
br.set_handle_redirect(True)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1000)
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]

formcount=0
for frm in br.forms():  
  if str(frm.attrs["id"])=="id-of-form":
    break
  formcount=formcount+1
br.select_form(nr=formcount)

with open('a filename', 'wb') as f:
    shutil.copyfileobj(br.submit(name='submit', label='value of submit button'), f)

如果重要的话;我正在运行 Mac OS X

4

1 回答 1

2

的返回值submit是一个类似文件的对象。您可以将数据复制到本地文件:

import shutil
with open('downloaded', 'wb') as f:
    shutil.copyfileobj(br.submit(), f)

不相关的是,您可以像这样缩短表单选择位:

br.select_form(predicate=lambda form: form.attrs['id'] == 'id-of-form')

这是一个完整的工作示例:

import mechanize
import shutil

br = mechanize.Browser()
br.open('http://stackoverflow.com/')
br.select_form(predicate=lambda form: form.attrs.get('id') == 'search')
br['q'] = '[python-mechanize]'
with open('search results.html', 'wb') as f:
    shutil.copyfileobj(br.submit(), f)
于 2013-05-19T01:41:38.047 回答