2

我有以下代码:

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)

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

4

1 回答 1

3

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

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

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

于 2013-03-25T21:43:00.343 回答