最近在练习python 的模拟登陆。。终于读取到cookie值跟session值了。。很开心
这是用chrome 抓到的包内容
Request URL:http://uzone.univs.cn/sso.action
Request Method:POST
Status Code:200 OK
Request Headersview parsed
POST /sso.action HTTP/1.1
Host: uzone.univs.cn
Connection: keep-alive
Content-Length: 56
Accept: */*
Origin: http://uzone.univs.cn
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.69 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://uzone.univs.cn/sso.action
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8
Cookie: JSESSIONID=A2A8BD9404CCB9918C7DA27EC36D29F9.uzone1; uid=58F205B6DAF45575; sessionid=A2A8BD9404CCB9918C7DA27EC36D29F9.uzone1; nickName=%CD%F5%C0%F6; __utma=85212998.529232546.1382107604.1382107604.1382107604.1; __utmb=85212998.2.10.1382107604; __utmc=85212998; __utmz=85212998.1382107604.1.1.utmcsr=uzone.univs.cn|utmccn=(referral)|utmcmd=referral|utmcct=/; Hm_lvt_1d5974eb1aab9d67910db50b9a0bc35b=1382107606; Hm_lpvt_1d5974eb1aab9d67910db50b9a0bc35b=1382107606; PHPSESSID=0qkr7d5h7qnvo01olsr64ql795; cmstop_auth=GABpADgAaAAUAHsAXgBWAAQABAACAAUABgACAF4AXgBSAFQAFABiAAAACABbAF4AAAA%3D; cmstop_userid=87383; cmstop_username=1346934661214312; cmstop_rememberusername=1346934661214312; Hm_lvt_135fffa8e25338079d2af5e63eea80ca=1382100680,1382107606; Hm_lpvt_135fffa8e25338079d2af5e63eea80ca=1382107616; __asc=23392cb4141cc086a9fb07ee14c; __auc=23392cb4141cc086a9fb07ee14c
Form Dataview parsed -------------- 主要的东东
gUser.loginName=AAA&gUser.password=888888
Response Headersview parsed
HTTP/1.1 200 OK
Date: Fri, 18 Oct 2013 14:48:07 GMT
Server: Apache/2.4.2 (Unix) mod_jk/1.2.37
P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"
Set-Cookie: spaceAutoLogonId=""; Expires=Fri, 18-Oct-2013 14:48:17 GMT
Set-Cookie: spaceAutoLogonFlag=""; Expires=Fri, 18-Oct-2013 14:48:17 GMT
Set-Cookie: uid=58F205B6DAF45575; Domain=univs.cn
Set-Cookie: sessionid=A2A8BD9404CCB9918C7DA27EC36D29F9.uzone1; Domain=univs.cn
Set-Cookie: nickName=%CD%F5%C0%F6; Domain=univs.cn
Content-Language: zh-CN
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 107
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/xml;charset=GBK
Dataview parsed 是用户提交的表单。
Rseponse Headersview parsed 是服务器返回过来的一系列信息。。包含cookie
下面是python脚本代码
import urllib,urllib2,cookielib
#from bs4 import BeautifulSoup
import urllib2,urllib
#尝试的网站
Target='http://uzone.univs.cn/sso.action'
UserName=''
PassWord=''
#进行 urlencode -> 弄成符合要求的get
def getPost(Username,Password):
return 'gUser.loginName='+Username+'&gUser.password='+Password
print getPost(UserName,PassWord)
myCookie=urllib2.HTTPCookieProcessor(cookielib.CookieJar());
data=getPost(UserName,PassWord)
response=urllib2.urlopen(Target,data)
cookies=response.headers["Set-cookie"]
#print cookies
cookie=cookies[cookies.index("sessionid="):]
#print cookie
session = cookie[10:cookie.index(";")+1]
#print session