python - 使用 python mechanize 库登录 https 站点

标签 python https mechanize

我有以下代码:

import requests
import sys
import urllib2
import re
import mechanize
import cookielib
#import json
#import imp
#print(imp.find_module("requests"))
#print(requests.__file__)
EMAIL = "******"
PASSWORD = "*******"

URL = 'https://www.imleagues.com/Login.aspx'
address = "http://www.imleagues.com/School/Team/Home.aspx?Team=27d6c31187314397b00293fb0cfbc79a"
br = mechanize.Browser()
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)

br.add_password(URL, EMAIL, PASSWORD)
br.open(URL)

#br.open(URL)
#br.select_form(name="aspnetForm")
#br.form["ctl00$ContentPlaceHolder1$inUserName"] = EMAIL
#br.form["ctl00$ContentPlaceHolder1$inPassword"] = PASSWORD
#response = br.submit()
#br= mechanize.Browser()
site = br.open(address)

    # Start a session so we can have persistant cookies
#session = requests.Session()

    # This is the form data that the page sends when logging in
#login_data = {
 #   'ctl00$ContentPlaceHolder1$inUserName': EMAIL,
 #   'ctl00$ContentPlaceHolder1$inPassword': PASSWORD,
  #  'aspnetFrom': 'http://www.imleagues.com/Members/Home.aspx',
#}
#URL_post = 'http://www.imleagues.com/Members/Home.aspx'
    # Authenticate
#r = session.post(URL, data=login_data)

    # Try accessing a page that requires you to be logged in
#r = session.get('http://www.imleagues.com/School/Team/Home.aspx?Team=27d6c31187314397b00293fb0cfbc79a')



website = site.read()


f = open('crypt.txt', 'wb')
f.write(website)

#print(website_html)

我正在尝试登录此网站以监控游戏时间并确保它们不会(再次)对我进行更改。我尝试了各种方法来做到这一点,大多数都在上面注释掉了,但所有这些方法都将我重定向回登录页面。有任何想法吗?谢谢。

最佳答案

正如我在给定网站中看到的登录按钮不在 submit 中标签。登录是 javascript 函数

<a ... href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$btnLogin','')" </a>

mechanize无法处理 JavaScript。我遇到了非常类似的问题,并想出了使用 Spynner 的解决方案。 它是 headless 网络浏览器。因此,您可以完成与使用 mechanize 相同的任务,并且它具有 JavaScript 支持。

关于python - 使用 python mechanize 库登录 https 站点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15625008/

相关文章:

python - 不同列名的 WHERE 子句

jsp - 使用 Spring Security 将 https 卸载到负载均衡器

python - 在 python 中以编程方式打开页面

在 http 和 https 上具有自定义绑定(bind)的 WCF

python - 如何使用 Mechanize 从 Google 的搜索结果中获取图像

python - Python 3.x 的 Mechanize

python,插入同一行两次时测试不会失败

python - 无法在 Python 中定义基本函数

python - python 中的成员资格测试比 set() 更快

https - 具有相同 header 的 firefox 和 luasocket 之间的差异