您的位置:首页 > 编程语言 > Python开发

python sina 围脖登陆

2013-03-17 20:32 225 查看
importre
importjson
importurllib
importbase64
importhashlib
importrequests

WBCLIENT='ssologin.js(v.1.3.18)'
sha1=lambdax:hashlib.sha1(x).hexdigest()

defwblogin(username,password):
session=requests.Session()

#headers={
#'User-Agent':'Mozilla/5.0(WindowsNT5.1)AppleWebKit/537.1(KHT'
#'ML,likeGecko)Chrome/21.0.1180.89Safari/537.1'
#}
#)
resp=session.get(
'http://login.sina.com.cn/sso/prelogin.php?entry=weibo&callback=sina'
'SSOController.preloginCallBack&su=%s&client=%s'%
(base64.b64encode(username),WBCLIENT)
)
pre_login_str=re.match(r'[^{]+({.+?})',resp.content).group(1)
pre_login_json=json.loads(pre_login_str)
data={
'entry':'weibo',
'gateway':1,
'from':'',
'savestate':7,
'useticket':1,
'ssosimplelogin':1,
'su':base64.b64encode(urllib.quote(username)),
'service':'miniblog',
'servertime':pre_login_json['servertime'],
'nonce':pre_login_json['nonce'],
'pcid':pre_login_json['pcid'],
'vsnf':1,
'vsnval':'',
'pwencode':'wsse',
'sp':sha1(sha1(sha1(password))+
str(pre_login_json['servertime'])+
pre_login_json['nonce']),
'encoding':'UTF-8',
'url':'http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.si'
'naSSOController.feedBackUrlCallBack',
'returntype':'META'
}
resp=session.post(
'http://login.sina.com.cn/sso/login.php?client=%s'%WBCLIENT,
data=data
)
login_url=re.search(r'replace\([\"\']([^\'\"]+)[\"\']',
resp.content).group(1)
resp=session.get(login_url)
login_str=re.match(r'[^{]+({.+?}})',resp.content).group(1)
returnsession,json.loads(login_str)

if__name__=='__main__':
frompprintimportpprint
session,login_info=wblogin('xxxxxxx@qq.com','xxxxxx')
uid=login_info['userinfo']['uniqueid']
url="http://weibo.com/u/"+uid
resp=session.get(url)
printresp.content


智普教育《python小弟编辑》:http://www.jeapedu.com/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: