到目前为止,我有机械化代码来执行此操作:
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
答案 0 :(得分: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)