Python Auto Fill with Mechanize

后端 未结 2 1631
無奈伤痛
無奈伤痛 2020-12-16 07:44

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

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-16 08:30

    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.

提交回复
热议问题