1. "su"的加密算法,su是username经过BASE64计算得来的:
def get_encodename(name): # name must be string username_quote = quote_plus(str(name)) username_base64 = base64.b64encode(username_quote.encode("utf-8")) return username_base64.decode("utf-8")
2.获取sp的值,就是密码rsa的加密值
1.首先访问http://login.sina.com.cn/sso/prelogin.php?entry=weibo&callback=sinaSSOController.preloginCallBack&su=上面的su字符串=&rsakt=mod&checkpin=1&client=ssologin.js(v1.4.18)&_=时间戳,获取json字符串
def get_server_data(su,session,proxy): pre_url = "http://login.sina.com.cn/sso/prelogin.php?entry=weibo&callback=sinaSSOController.preloginCallBack&su=" pre_url = pre_url + su + "&rsakt=mod&checkpin=1&client=ssologin.js(v1.4.18)&_=" prelogin_url = pre_url + str(int(time.time() * 1000)) pre_data_res = session.get(prelogin_url, headers=headers, proxies=proxy) sever_data = ast.literal_eval(pre_data_res.content.decode("utf-8").replace("sinaSSOController.preloginCallBack", '')) return sever_data
2.获取url的post数据的一部分内容
{"retcode":0,"servertime":1526866235,"pcid":"gz-5a7050e9c71bbf284bca688fd7bd24ec57e1","nonce":"WPGNDA","pubkey":"EB2A38568661887FA1w3062882729293E5506350508E7F9AA3BB77F4333231490F915F6D63C55FE2F08A49B353F444AD3993CACC02DB784ABBB8E42A9B1BBFFFB38BE18D78E87A0E41B9B8F73A928EE0CCEE1F6739884B9777E4FE9E88A1BBE495927AC4A799B3181D6442443","rsakv":"1330428213","is_openlock":0,"lm":1,"smsurl":"https://login.sina.com.cn/sso/msglogin?entry=weibo&mobile=18810668312&s=5a68912sabf7e2d399ab4f878dc7","showpin":0,"exectime":27}
"showpin":表示是否需要验证码,1需要,0不需要
这一步为了获取下面几个参数的值
servertime = server_data["servertime"] nonce = server_data['nonce'] rsakv = server_data["rsakv"] pubkey = server_data["pubkey"]
然后进行rsa解密,导入rsa模块
def get_password(password, servertime, nonce, pubkey): rsa_publickey = int(pubkey, 16) key = rsa.PublicKey(rsa_publickey, 65537) #创建公钥 message = str(servertime) + ' ' + str(nonce) + ' ' + str(password) #创建公钥 message = message.encode("utf-8") passwd = rsa.encrypt(message, key) #加密 passwd = binascii.b2a_hex(passwd) #将加密信息转换为16进制。 return passwd
最后我们就可以构造post数据进行请求了
def login_no_pincode(name, password, session, server_data, proxy): post_url = 'http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.18)' servertime = server_data["servertime"] nonce = server_data['nonce'] rsakv = server_data["rsakv"] pubkey = server_data["pubkey"] sp = get_password(password, servertime, nonce, pubkey) data = { 'encoding': 'UTF-8', 'entry': 'weibo', 'from': '', 'gateway': '1', 'nonce': nonce, 'pagerefer': "", 'prelt': 67, 'pwencode': 'rsa2', "returntype": "META", 'rsakv': rsakv, 'savestate': '7', 'servertime': servertime, 'service': 'miniblog', 'sp': sp, 'sr': '1920*1080', 'su': get_encodename(name), 'useticket': '1', 'vsnf': '1', 'url': 'http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack' } return None, '', session