python - Mechanize submit a form that creates a dynamic flie that needs to be downloaded -


so far have mechanize code this:

goes site logs in submits form 

heres hit problems. need write response (a file) local file. pretty clueless far python interacting file system.

thanks in advance

edit: here of code have

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 frm in br.forms():     if str(frm.attrs["id"])=="id-of-form":     break   formcount=formcount+1 br.select_form(nr=formcount)  open('a filename', 'wb') f:     shutil.copyfileobj(br.submit(name='submit', label='value of submit button'), f) 

if matters; i'm running mac os x

the return value of submit file-like object. can copy data local file:

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

unrelatedly, can shorten form selection bit this:

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

here's 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]' open('search results.html', 'wb') f:     shutil.copyfileobj(br.submit(), f) 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -