Verifying br.submit() using Python's Mechanize module

走远了吗. 提交于 2019-12-08 02:14:52

问题


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:


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)


来源:https://stackoverflow.com/questions/11531958/verifying-br-submit-using-pythons-mechanize-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!