#coding:utf-8
import requests
import base64
import re
import urllib
import rsa
import json
import binascii
import time
session=0
uid=0
resp=0
def Login(username,password):
global session
global uid
global resp
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)'
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']
su = base64.b64encode(urllib.quote(username))
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)
resp = session.get(login_url[0])
uid = re.findall('"uniqueid":"(\d+)",',resp.content)[0]
def decode_content(content):
result = re.findall('<script>STK && STK.pageletM && STK.pageletM.view\((.*?)\)<\/script>',content)
for i in result:
r = i.encode("utf-8").decode('unicode_escape').encode("utf-8")
print r.replace("\/","/")
# 投票
def add_vote():
global session
global uid
global resp
votedata={
'item':'4',
'share':'1',
'poll_id':'*******',
'poll_category':'0',
'_t':'0'
}
headers={}
headers={'User-Agent':'Mozilla/5.0 (X11; Linux i686; rv:8.0) Gecko/20100101 Firefox/8.0',
'Referer':'http://vote.weibo.com/vid=*******'}
resp = session.post("http://vote.weibo.com/poll/joined",data=votedata,headers=headers)
print resp.status_code
# 发送微博
def add_new(content):
global uid
global resp
add_url = "http://weibo.com/aj/mblog/add?_wv=5&__rnd=%s770"% int(time.time())
add_data = {
'text':content,
'rank':0,
'rankid':'',
'location':'home',
'module':'stissue',
"hottopicid":"",
'_surl':'',
'pic_id':'',
'_t':0,
}
headers={}
headers ['set-cookie']= resp.headers['set-cookie']
headers['Referer'] = 'http://weibo.com/u/'+uid+'?topnav=1&wvr=5'
resp = session.post(add_url,data=add_data,headers=headers)
print resp.status_code
# 转发
# forward('3606151827013483', "转发")
def forward(mid,content):
global uid
global resp
forwardurl = "http://weibo.com/aj/mblog/forward?_wv=5&__rnd=%s"% int(time.time())
data = {'mid':mid, 'style-type':1, 'reason':content, 'rank':0, 'location':'mblog', '_t':0}
headers = {}
headers['set-cookie'] = resp.headers['set-cookie']
headers['Referer'] = 'http://weibo.com/u/'+uid+'?topnav=1&wv=5'
respon = session.post(forwardurl, data, headers=headers)
print respon.status_code
forward('3606151827013483', "转发")
# 关注
# followed('2898801847',uid)
def followed(dstuid,oid):
global uid
global resp
followedurl = "http://weibo.com/aj/f/followed?_wv=5&__rnd=%s"% int(time.time())
data = {'uid':dstuid, 'rank':0, 'location':'mblog', '_t':0,'f':0,
'oid':oid,
'nogroup':'false',
'challenge_uids':'',
'check_challenge_value':'',
'location':'home',
'refer_sort':'interest',
'refer_flag':'friend_bridge',
'loc':1,
}
headers = {}
headers['set-cookie'] = resp.headers['set-cookie']
headers['Referer'] = 'http://weibo.com/u/'+oid+'?topnav=1&wv=5'
respon = session.post(followedurl, data, headers=headers)
print respon.status_code
if __name__=="__main__":
fp=open("in.txt","r").readlines()
for line in fp:
line=line.split('\t')
print line[0],line[1]
try:
Login(line[0].strip(),line[1].strip())
add_vote()
except:
print "not login"
pass
else:
time.sleep(181)
从in.txt读入用户名 密码 以\t 分割修改*******为你要投票的id.每3分钟投一次,不会被封ip.20131016亲测可用
原理:转自http://uliweb.clkg.org/tutorial/view_chapter/191
1, 在提交POST请求之前, 需要GET 获取两个参数。
地址是:http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.3.18)
得到的数据中有 "servertime" 和 "nonce" 的值, 是随机的,其他值貌似没什么用。
2, 通过httpfox 观察POST 的数据, 参数较复杂,其中 “su" 是加密后的username, "sp"是加密后的password。"servertime" 和 ”nonce" 是上一步得到的。其他参数是不变的。
username 经过了BASE64 计算: username = base64.encodestring( urllib.quote(username) )[:-1];
password 经过了三次SHA1 加密, 且其中加入了 servertime 和 nonce 的值来干扰。
即: 两次SHA1加密后, 将结果加上 servertime 和 nonce 的值, 再SHA1 算一次。
3.用request的session登录
这样后来的每次访问都可以继承以前的session,系统会保存一些登录信息。
通过正则表达式,分离出 servertime,nonce。。。
转载自原文链接, 如需删除请联系管理员。
原文链接:最新 新浪微博 python 自动登陆 刷票 关注 发微博 转发,转载请注明来源!