Created
October 23, 2012 00:03
-
-
Save stephenLee/3935680 to your computer and use it in GitHub Desktop.
weibo_login script
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=utf8 | |
import urllib | |
import urllib2 | |
import cookielib | |
import base64 | |
import re | |
import json | |
import hashlib | |
import os | |
#login code from: http://www.douban.com/note/201767245/ | |
# cookie -> opener -> urllib2. | |
# 然后,urllib2的操作相关cookie会存在 | |
# 所以登陆成功之后,urllib2的操作会带有cookie信息,抓网页不会跳转到登陆页 | |
cookiejar = cookielib.LWPCookieJar() | |
cookie_support = urllib2.HTTPCookieProcessor(cookiejar) | |
opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler) | |
urllib2.install_opener(opener) | |
postdata = { | |
'entry': 'weibo', | |
'gateway': '1', | |
'from': '', | |
'savestate': '7', | |
'userticket': '1', | |
'ssosimplelogin': '1', | |
'vsnf': '1', | |
'vsnval': '', | |
'su': '', | |
'service': 'miniblog', | |
'servertime': '', | |
'nonce': '', | |
'pwencode': 'wsse', | |
'sp': '', | |
'encoding': 'UTF-8', | |
'url': 'http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack', | |
'returntype': 'META' | |
} | |
def get_servertime(): | |
url = 'http://login.sina.com.cn/sso/prelogin.php?entry=weibo&callback=sinaSSOController.preloginCallBack&su=dW5kZWZpbmVk&client=ssologin.js(v1.3.18)&_=1329806375939' | |
data = urllib2.urlopen(url).read() | |
p = re.compile('\((.*)\)') | |
try: | |
json_data = p.search(data).group(1) | |
data = json.loads(json_data) | |
servertime = str(data['servertime']) | |
nonce = data['nonce'] | |
return servertime, nonce | |
except: | |
print 'Get severtime error!' | |
return None | |
def get_pwd(pwd, servertime, nonce): | |
pwd1 = hashlib.sha1(pwd).hexdigest() | |
pwd2 = hashlib.sha1(pwd1).hexdigest() | |
pwd3_ = pwd2 + servertime + nonce | |
pwd3 = hashlib.sha1(pwd3_).hexdigest() | |
return pwd3 | |
def get_user(username): | |
username_ = urllib.quote(username) | |
username = base64.encodestring(username_)[:-1] | |
return username | |
def login(username, pwd): | |
url = 'http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.3.18)' | |
try: | |
servertime, nonce = get_servertime() | |
except: | |
return | |
global postdata | |
postdata['servertime'] += servertime | |
postdata['nonce'] += nonce | |
postdata['su'] += get_user(username) | |
postdata['sp'] += get_pwd(pwd, servertime, nonce) | |
postdata = urllib.urlencode(postdata) | |
headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux i686; rv:8.0) Gecko/20100101 Firefox/8.0'} | |
req = urllib2.Request( | |
url=url, | |
data=postdata, | |
headers=headers | |
) | |
result = urllib2.urlopen(req) | |
text = result.read() | |
p = re.compile('location\.replace\(\'(.*?)\'\)') | |
try: | |
login_url = p.search(text).group(1) | |
#print login_url | |
urllib2.urlopen(login_url) | |
print "登录成功!" | |
except: | |
print 'Login error!' | |
#update status | |
def update_status(status): | |
main_url = "http://weibo.com/reddit4ml" | |
res = urllib2.urlopen(main_url) | |
payload = { | |
'text': status.encode('utf-8'), | |
'location': 'home', | |
'module': 'stissue', | |
'_t': 0 | |
} | |
headers = { | |
'User-Agent': 'Mozilla/5.0 (X11; Linux i686; rv:8.0) Gecko/20100101 Firefox/8.0', | |
'Referer': main_url | |
} | |
data = urllib.urlencode(payload) | |
req = urllib2.Request( | |
url = 'http://weibo.com/aj/mblog/add', | |
data = data, | |
headers = headers | |
) | |
try: | |
res = urllib2.urlopen(req) | |
text = res.read() | |
print text | |
except: | |
print 'update weibo status error' | |
def test(): | |
username = 'Your username' | |
pwd = 'Your password' | |
login(username, pwd) | |
status = u'测试一下' | |
update_status(status) | |
if __name__ == '__main__': | |
test() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment