# 导入模块 urllib2
import urllib
import urllib2
import cookielib
from bs4 import BeautifulSoup
import re
import sys;
reload(sys);
sys.setdefaultencoding('utf8');
login_url='http://***/cas/login?service=http%3A%2F%2F***%2Fmamplatform%2Flogon.action'
url = 'http://***/mamplatform/index.action'
html1=urllib2.urlopen(login_url).read()
soup1=BeautifulSoup(html1)
lt=soup1.select("[name=lt]")[0]["value"]
execution=soup1.select("[name=execution]")[0]["value"]
cj=cookielib.CookieJar()
opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj),urllib2.HTTPHandler)
urllib2.install_opener(opener)
h=urllib2.urlopen(url).read()
headers = {
'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)',
'Accept': 'application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, */*',
'Connection': 'keep-alive',
'Referer':'http://***/cas/login?service=http%3A%2F%2F***%2Fmamplatform' }
postData=urllib.urlencode({
'username':'aa',
'password':'aa',
'password-prompt':'请输入密码',
'lt':lt,
'execution':execution,
'_eventId':'submit'
})
print postData
req = urllib2.Request(login_url, postData,headers=headers)
response = opener.open( req )
print response.info()
doc = response.read()
req2 = urllib2.Request(url)
response2 = opener.open( req2 )
print response2.info()
doc2 = response2.read()
print 'old url:'+url
print 'real url:'+response2.geturl()
代码如上所示,不知哪里有问题,一直在登陆界面,通过HTTPWATCH抓包如下