The return value of submit
is a file-like object. You can copy the data to a local file:
import shutil
with open('downloaded', 'wb') as f:
shutil.copyfileobj(br.submit(), f)
Unrelatedly, you can shorten the form selection bit like this:
br.select_form(predicate=lambda form: form.attrs['id'] == 'id-of-form')
Here's a full working example:
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)