-
-
Save zfz/b2580bdb9bdfc007c6d2 to your computer and use it in GitHub Desktop.
Python登录新浪微博,修改后15年10月暂时可用
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
requests==2.4.3 | |
rsa==3.1.4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from __future__ import absolute_import, division, print_function, unicode_literals | |
import re | |
import json | |
import base64 | |
import binascii | |
import rsa | |
import requests | |
import logging | |
logging.basicConfig(level=logging.DEBUG) | |
WBCLIENT = 'ssologin.js(v1.4.18)' | |
user_agent = ( | |
'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.11 (KHTML, like Gecko) ' | |
'Chrome/20.0.1132.57 Safari/536.11' | |
) | |
session = requests.session() | |
session.headers['User-Agent'] = user_agent | |
def encrypt_passwd(passwd, pubkey, servertime, nonce): | |
key = rsa.PublicKey(int(pubkey, 16), int('10001', 16)) | |
message = str(servertime) + '\t' + str(nonce) + '\n' + str(passwd) | |
passwd = rsa.encrypt(message.encode('utf-8'), key) | |
return binascii.b2a_hex(passwd) | |
def wblogin(username, password): | |
resp = session.get( | |
'http://login.sina.com.cn/sso/prelogin.php?' | |
'entry=sso&callback=sinaSSOController.preloginCallBack&' | |
'su=%s&rsakt=mod&client=%s' % | |
(base64.b64encode(username.encode('utf-8')), WBCLIENT) | |
) | |
pre_login_str = re.match(r'[^{]+({.+?})', resp.text).group(1) | |
pre_login = json.loads(pre_login_str) | |
pre_login = json.loads(pre_login_str) | |
data = { | |
'entry': 'weibo', | |
'gateway': 1, | |
'from': '', | |
'savestate': 7, | |
'userticket': 1, | |
'ssosimplelogin': 1, | |
'su': base64.b64encode(requests.utils.quote(username).encode('utf-8')), | |
'service': 'miniblog', | |
'servertime': pre_login['servertime'], | |
'nonce': pre_login['nonce'], | |
'vsnf': 1, | |
'vsnval': '', | |
'pwencode': 'rsa2', | |
'sp': encrypt_passwd(password, pre_login['pubkey'], | |
pre_login['servertime'], pre_login['nonce']), | |
'rsakv' : pre_login['rsakv'], | |
'encoding': 'UTF-8', | |
'prelt': '115', | |
'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.text).group(1) | |
login_url = re.search('replace\\(\'([^\']+)\'\\)', resp.text).group(1) | |
resp = session.get(login_url) | |
#login_str = re.match(r'[^{]+({.+?}})', resp.text).group(1) | |
login_str = re.search('\((\{.*\})\)', resp.text).group(1) | |
return json.loads(login_str) | |
if __name__ == '__main__': | |
print(json.dumps(wblogin('xxxx', 'xxxx'), ensure_ascii=False)) | |
# timeline | |
print(session.get('http://weibo.com/').text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment