2013-03-25 78 views
2

我有以下代碼:登錄到HTTPS站點使用python機械化庫

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) 

我試圖登錄到這個網站來監視遊戲時間,並確保他們不會對我改變(再次)。我已經嘗試過多種方式來做到這一點,上面評論最多,但是他們都將我重定向回登錄頁面。有任何想法嗎?謝謝。

回答

3

正如我在給定網站中看到的登錄按鈕不在submit標記中。登錄是javascript函數

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

mechanize無法處理JavaScript。我遇到了非常類似的問題,並提出了使用Spynner的解決方案。 它是無頭網頁瀏覽器。因此,您可以使用機械化來完成相同的任務,並且具有JavaScript支持。

+0

這樣做更有意義。謝謝。 – 2013-03-25 21:58:01

+0

關於如何從下拉按鈕(javascript)中選擇選項的任何想法?我已經想出瞭如何填寫我的用戶名和密碼,但我現在需要選擇我的學校。 – 2013-03-26 00:58:11

+0

你可以在spinner中運行js代碼,我猜它比在python中更容易。它看起來是這樣的:''browser.runjs('$(「[schid = e002364c7b414cfd8195be06fb7707ba]」)。click()');''schid'是'html'中特定學校'li'項目的屬性。這裏是要點我已經嘗試過:https://gist.github.com/zygisx/5243735 – 2013-03-26 07:44:16