1. 程式人生 > >python爬蟲自動登入武漢大學校園網

python爬蟲自動登入武漢大學校園網

#coding:utf-8
__author__ = 'zy'
import urllib2
import cookielib
import urllib
import re
import sys
'''模擬登入'''

PostUrl = "http://202.114.64.105:8080/eportal/userV2.do?method=login&param=true&wlanuserip=ad1052f9d875d2c2202fe4aa93aae07c&wlanacname=29185648f4390d7911ef4b72391e17a9&ssid=&nasip=07e38f2323f330cd5ffcc3a203a63100&snmpagentip=&mac=e597b13dde22b3befb3e410e4564c6b7&t=wireless-v2&url=031144d9c31b76483cfae87a655ab8cffd310d1a5f2b3844e92a4e88af3808e6&apmac=&nasid=29185648f4390d7911ef4b72391e17a9&vid=8dcf080f7f54bb82&port=bb13fa65ec170962&nasportid=ac41d60d7f138208bea8d2af9b4c564cc1d0e0e34515f94a78740bbe9bdcd095&username=2014301610386&pwd=197510"
# 驗證碼地址和post地址 cookie = cookielib.CookieJar() handler = urllib2.HTTPCookieProcessor(cookie) opener = urllib2.build_opener(handler) # 將cookies繫結到一個opener cookie由cookielib自動管理 username = '2014301610***' password = '******' # 使用者名稱和密碼 postData = { 'is_auto_land':"false", "usernameHidden":"2014301610386"
, "username_tip":"Username", "username":username, 'strTypeAu':"", 'uuidQrCode':"", 'authorMode':"", 'pwd_tip':"Password", 'pwd':password, } # 根據抓包資訊 構造表單 headers = { 'Accept':"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 'Accept-Encoding':'gzip, deflate, br'
, 'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3', 'Connection': 'keep-alive', 'Content-Length':'37', 'Host':'202.114.64.105:8080', 'Cookie':cookie, 'Referer':'http://202.114.64.105:8080/eportal/index.jsp?wlanuserip=ad1052f9d875d2c2202fe4aa93aae07c&wlanacname=29185648f4390d7911ef4b72391e17a9&ssid=&nasip=07e38f2323f330cd5ffcc3a203a63100&snmpagentip=&mac=e597b13dde22b3befb3e410e4564c6b7&t=wireless-v2&url=031144d9c31b76483cfae87a655ab8cffd310d1a5f2b3844e92a4e88af3808e6&apmac=&nasid=29185648f4390d7911ef4b72391e17a9&vid=8dcf080f7f54bb82&port=bb13fa65ec170962&nasportid=ac41d60d7f138208bea8d2af9b4c564cc1d0e0e34515f94a78740bbe9bdcd095', #'Upgrade-Insecure-Requests':1, 'User-Agent': 'User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.59 Safari/537.36', } # 根據抓包資訊 構造headers data = urllib.urlencode(postData) # 生成post資料 ?key1=value1&key2=value2的形式 request = urllib2.Request(PostUrl, data, headers) # 構造request請求 #try: response = opener.open(request).read() # 由於該網頁是gb2312的編碼,所以需要解碼 print response.decode('utf-8').encode('gbk') # 列印登入後的頁面 #except (urllib2.HTTPError),e: # print e.code66 # 利用之前存有cookie的opener登入頁面