python 2.7
#!/usr/bin/env python
# coding=utf-8
import time
import requests
import urllib
import base64
import rsa
import binascii
import re
# 构造Request headers
agent = 'Mozilla/5.0 (X11; Linux x86_64; rv:49.0) Gecko/20100101 Firefox/49.0'
host = 'weibo.com'
accept = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
accept_language = 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3'
accept_encoding = 'gzip, deflate'
content_type = 'application/x-www-form-urlencoded'
headers = {
'User-Agent': agent
}
# 得到session
session = requests.session()
# 访问初始页
index_url = "http://weibo.com/login.php"
session.get(index_url, headers=headers, timeout=4)
'''''
1. 分析 post 提交的信息
2. 利用 httpfox 抓取 http://i.sso.sina.com.cn/js/ssologin.js,查看各个参数的构造方法
'''
def get_su(username):
'''''
js: sinaSSOEncoder.base64.encode(urlencode(username))
urlencode -> encodeURIComponent()
'''
# quote_plus : 将用于分隔uri组件的标点符号用16进制转义序列替换
username_quote = urllib.quote_plus(username)
username_base64 = base64.b64encode(username_quote.encode('utf-8'))
return username_base64.decode('utf-8')
'''''
servertime-nonce-rsakv
'''
def get_server_data(su):
'''''
js: prelogin 函数
'''
pre_url = 'http://login.sina.com.cn/sso/prelogin.php?entry=weibo&callback=sinaSSOController.preloginCallBack&su='
pre_url = pre_url + su + '&rsakt=mod&client=ssologin.js(v1.4.18)&_='
pre_url = pre_url + str(int(time.time()))
# 从session 得到值
pre_data_res = session.get(pre_url, headers=headers)
sever_data = eval(pre_data_res.content.decode("utf-8").replace("sinaSSOController.preloginCallBack", ''))
# print sever_data
return sever_data;
def get_password(password, servertime, nonce, pubkey):
rsaPublickey = int(pubkey, 16)
key = rsa.PublicKey(rsaPublickey, 65537) # 公钥
# js: password = RSAKey.encrypt([me.servertime, me.nonce].join("\t") + "\n" + password)
message = str(servertime) + '\t' + str(nonce) + '\n' + str(password)
message = message.encode('utf-8')
# 加密
passwd = rsa.encrypt(message, key)
# 16进制
passwd = binascii.b2a_hex(passwd)
return passwd;
def login(username, password):
su = get_su(username)
sever_data = get_server_data(su)
servertime = sever_data['servertime']
nonce = sever_data['nonce']
rsakv = sever_data['rsakv']
pubkey = sever_data['pubkey']
# 是否安全登录
# showpin = server_data['showpin']
password_secret = get_password(password, servertime, nonce, pubkey)
postdata = {
'entry': 'weibo',
'gateway': '1',
'from': '',
'savestate': '7',
'useticket': '1',
'pagerefer': "http://login.sina.com.cn/sso/logout.php?entry=miniblog&r=http%3A%2F%2Fweibo.com%2Flogout.php%3Fbackurl",
'vsnf': '1',
'su': su,
'service': 'miniblog',
'servertime': servertime,
'nonce': nonce,
'pwencode': 'rsa2',
'rsakv': rsakv,
'sp': password_secret,
'sr': '1366*768',
'encoding': 'UTF-8',
'prelt': '115',
'url': 'http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack',
'returntype': 'META'
}
login_url = 'http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.18)'
login_page = session.post(login_url, data=postdata, headers=headers)
login_loop = (login_page.content.decode('GBK'))
# 新浪通行证
# print login_loop
pa = r'location\.replace\([\'"](.*?)[\'"]\)'
loop_url = re.findall(pa, login_loop)[0] #
# print(loop_url)
login_index = session.get(loop_url, headers=headers)
uuid = login_index.text
# 得到 uuid
uuid_pa = r'"uniqueid":"(.*?)"'
uuid_res = re.findall(uuid_pa, uuid, re.S)[0]
# 登陆界面
web_weibo_url = "http://weibo.com/%s/profile?topnav=1&wvr=6&is_all=1" % uuid_res
weibo_page = session.get(web_weibo_url, headers=headers)
# 得到登陆 用户名
weibo_pa = r'<title>(.*?)</title>'
# print(weibo_page.content.decode("utf-8"))
userID = re.findall(weibo_pa, weibo_page.content.decode("utf-8", 'ignore'), re.S)[0]
print(u"欢迎你 %s, 模拟登录微博" % userID)
# 留言
def leave_word():
# 只是18842471360用户 AV女优 关注栏目中更新的微博留言
l_headers = {
'Host': host,
'User-Agent': agent,
'Accept': accept,
'Accept-Language': accept_language,
'Accept-Encoding': accept_encoding,
'Content-Type': content_type,
'X-Requested-With': 'XMLHttpRequest',
'Referer': 'http://weibo.com/mygroups?gid=4050356944274918&wvr=6&leftnav=1',
'Connection': 'keep-alive'
}
session.get('http://weibo.com/u/6078708396/home?topnav=1&wvr=6')
#time.sleep(5)
url_av = 'http://weibo.com/mygroups?gid=4050356944274918&wvr=6&leftnav=1'
av_page = session.get(url=url_av)
# with open('leave1.html', 'w') as l:
# l.write(av_page.text.encode('utf-8','ignore'))
# time.sleep(5)
# mrid=\"rid=0_0_202_2669571729310635176\"
rid_pa = r'mrid=\\\"rid=(.*?)\\\"'
rid_list = re.findall(rid_pa, av_page.text)
# rid = rid_list[0] # 友田彩也香
# mid=\"4047260247292112\" action-type
mid_pa = r'mid=\\\"(\d{16})\\\" action-type=\\\"feed_list_item\\\"'
mid_list = re.findall(mid_pa, av_page.text)
# mid = mid_list[0]
# 我的关注
post_data = {
'act':'post',
'mid':'',
'uid':'6078708396',
'forward':'0',
'isroot':'0',
'content':'',
'location':'v6_group_content_home',
'module':'scommlist',
'group_source':'group_gid',
'rid':'',
'pdetail':'',
'_t':'0'
}
'''
for m in mid_list:
print m
for r in rid_list:
print r
print len(mid_list),len(rid_list)
os._exit(0)
'''
# 发重复的话,会被屏蔽id
for i in xrange(len(rid_list)):
rnd = '%d' % (time.time() * 1000)
leave_url = 'http://weibo.com/aj/v6/comment/add?ajwvr=6&__rnd=' + rnd
print 'url:',leave_url
post_data['mid'] = mid_list[i]
post_data['rid'] = rid_list[i]
print 'mid',post_data['mid'], 'rid',post_data['rid']
# 10 次遍历立马被封id
for j in range(1):
# msg = raw_input('留言:')
msg = '...'+str(i)
post_data['content'] = msg
leave_page = session.post(leave_url, data=post_data, headers=l_headers)
print 'go...',i
time.sleep(10)
# with open('leave.html', 'w') as l:
# l.write(leave_page.text.encode('utf-8','ignore'))
print 'end...'
if __name__ == "__main__":
# username = raw_input('用户名:')
# password = raw_input('密码:')
'''
18940085029
96----..**
'''
username = '18842471360'
password = ''
login(username, password)
time.sleep(10)
leave_word()