Matt D Matt D - 7 days ago 5
Python Question

Login with python mechanize

I am trying to create a python script that uses mechanize to submit data to a web form.

This is what I have so far

import mechanize


username = <my_username>
password = <my_password>
url = <my_url>


print "opening browser"
br = mechanize.Browser()
print "opening url...please wait"
br.open(url)
print br.title()
print "selecting form"
br.select_form(name='Login')
br['UserID'] = username
br['PassPhrase'] = password
print "submitting form"
br.submit()


I'm stuck on what to do now. Does
br
now contain the next webpage than comes up after submitting the form? Is there a way I can print the
html
in
br
to check what webpage is in it? (I cant use
print br.title()
because both pages have the same title)

Answer

Try this at the end:

response = br.submit()
pageSource=response.read()

pageSource should give you the html-source of the post-login page.

Comments