使用Python的Mechanize模块验证br.submit() [英] Verifying br.submit() using Python's Mechanize module

查看:74
本文介绍了使用Python的Mechanize模块验证br.submit()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只需尝试使用机械化登录网站.当我打印"br.form"时,我可以看到我的凭据输入到我的表单中.但是我不知道如何正确地提交表格.

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.

我使用"br.submit()",并尝试通过打印br.title()来验证它是否已进入下一页,但是显示的标题是用于登录屏幕的,而不是登录后的屏幕.

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()

推荐答案

这可能使您对正在发生的事情有更好的了解.

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)

这篇关于使用Python的Mechanize模块验证br.submit()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆