Could someone help me or share some code to auto fill a login with mechanize (http://wwwsearch.sourceforge.net/mechanize/)? I want to make a python script to log me into my favorite sites when I run it.
Thanks!
解决方案
This will help you to login to one site and download a page for example:
import mechanize
br=mechanize.Browser()
br.open('http://www.yourfavoritesite.com')
br.select_form(nr=0) #check yoursite forms to match the correct number
br['Username']='Username' #use the proper input type=text name
br['Password']='Password' #use the proper input type=password name
br.submit()
br.retrieve('https://www.yourfavoritesite.com/pagetoretrieve.html','yourfavoritepage.html')
This script presumes that your login form is the first of the page and the input names are Username and Password.
You could also select your form by name with:
br.select_form(name="thisthing")
Please, adapt this script to your favorite site login page.
As well pointed by AlexMartelli, this script should be generalized to handle different sites with some config parameters.