【问题标题】:Verifying br.submit() using Python's Mechanize module使用 Python 的 Mechanize 模块验证 br.submit()
【发布时间】:2012-07-17 22:53:45
【问题描述】:

只是尝试使用 mechanize 登录网站。当我打印“br.form”时,我可以看到我的凭据输入到我的表单中。但我不知道如何正确提交表单。

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

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

【问题讨论】:

    标签: python-2.7 mechanize mechanize-python


    【解决方案1】:

    这可能会让您更好地了解正在发生的事情。

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

    一般来说,在 mechanize 中启用调试可能会有所帮助:

    br.set_debug_http(True)
    br.set_debug_responses(True)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-20
      • 2013-07-06
      • 2014-04-09
      • 2021-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多