Verifying br.submit() using Python's Mechanize

2019-05-21 12:00发布

Just trying to login to a website using mechanize. When I print "br.form", I can see my credentials entered into my form. But I do not know how to actually submit the form properly.

I use "br.submit()" and attempt to verify it has proceeded to the next page by printing the br.title(), but the title appearing is for the login screen, and not the post-login screen.

import mechanize
from time import sleep
def reportDownload():

    # Prompt for login credentials
    print("We require your credentials.")
    Username = raw_input("Please enter your username. ")
    Password = raw_input("Please input your password. ").encode('base64')

    URL = "https://login.xxxxxxxxx.com/"    
    br = mechanize.Browser()
    br.open(URL)    
    br.select_form(nr=0)

    br['username'] = Username
    br['pw'] = Password.decode('base64')

    print br.form       
    # Login 
    br.submit() 

    # print page title to confirm proper login
    print br.title()

reportDownload()

1条回答
2楼-- · 2019-05-21 13:01

This might give you a better picture of what's going on.

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

It would probably help in general to enable debugging in mechanize:

br.set_debug_http(True)
br.set_debug_responses(True)
查看更多
登录 后发表回答