最新消息:

Python 自动登陆 新浪微博

python admin 3661浏览 0评论

第一步:

登陆weibo我们首先需要从: url_prelogin = ‘http://login.sina.com.cn/sso/prelogin.php?entry=weibo&callback=sinaSSOController.preloginCallBack&su=&rsakt=mod&client=ssologin.js(v1.4.5)&_=1364875106625’

取得 4个值,主要是对本地明文密码进行加密。

servertime = data['servertime']
nonce      = data['nonce']
pubkey     = data['pubkey']
rsakv      = data['rsakv']

第二步:

加密用户名(su)

su = base64.b64encode(urllib.quote(username))

加密密码(sp):

rsaPublickey= int(pubkey,16)
key = rsa.PublicKey(rsaPublickey,65537)
message = str(servertime) +'t' + str(nonce) + 'n' + str(password)
sp = binascii.b2a_hex(rsa.encrypt(message,key))

加密后的数据提交

postdata = {
                    'entry': 'weibo',
                    'gateway': '1',
                    'from': '',
                    'savestate': '7',
                    'userticket': '1',
                    'ssosimplelogin': '1',
                    'vsnf': '1',
                    'vsnval': '',
                    'su': su,
                    'service': 'miniblog',
                    'servertime': servertime,
                    'nonce': nonce,
                    'pwencode': 'rsa2',
                    'sp': sp,
                    'encoding': 'UTF-8',
                    'url': 'http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack',
                    'returntype': 'META',
                    'rsakv' : rsakv,
                    }

第三步登陆:


url_login = 'http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.5)'
resp = session.post(url_login,data=postdata)

完整代码:

#coding:utf-8
import requests
import base64
import re
import urllib
import rsa
import json
import binascii
username = 'xxx'
password = 'xxx'
session = requests.Session()
url_prelogin = 'http://login.sina.com.cn/sso/prelogin.php?entry=weibo&callback=sinaSSOController.preloginCallBack&su=&rsakt=mod&client=ssologin.js(v1.4.5)&_=1364875106625'
url_login = 'http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.5)'
#get servertime,nonce, pubkey,rsakv
resp = session.get(url_prelogin)
json_data  = re.search('((.*))', resp.content).group(1)
data       = json.loads(json_data)
servertime = data['servertime']
nonce      = data['nonce']
pubkey     = data['pubkey']
rsakv      = data['rsakv']
# calc su
su  = base64.b64encode(urllib.quote(username))
#calc sp
rsaPublickey= int(pubkey,16)
key = rsa.PublicKey(rsaPublickey,65537)
message = str(servertime) +'t' + str(nonce) + 'n' + str(password)
sp = binascii.b2a_hex(rsa.encrypt(message,key))
postdata = {
                    'entry': 'weibo',
                    'gateway': '1',
                    'from': '',
                    'savestate': '7',
                    'userticket': '1',
                    'ssosimplelogin': '1',
                    'vsnf': '1',
                    'vsnval': '',
                    'su': su,
                    'service': 'miniblog',
                    'servertime': servertime,
                    'nonce': nonce,
                    'pwencode': 'rsa2',
                    'sp': sp,
                    'encoding': 'UTF-8',
                    'url': 'http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack',
                    'returntype': 'META',
                    'rsakv' : rsakv,
                    }
resp = session.post(url_login,data=postdata)
login_url = re.findall('replace("(.*)")',resp.content)
#print login_url
resp = session.get(login_url[0])
print resp.content
uid = re.findall('"uniqueid":"(d+)",',resp.content)[0]
url = "http://weibo.com/u/"+uid
resp = session.get(url)
print resp.content

提示:新浪有登入保护,有时会出现验证码而导致无法登陆,可以新浪账户设置=>账户安全去掉验证码

 

转载请注明:爱开源 » Python 自动登陆 新浪微博

您必须 登录 才能发表评论!