新浪微博4周年抽红包
Python v3.5
使用到了requests,pyquery模块
2016年6月16日之前可以连续刷,之后一次只显示4个红包
# coding:utf8
"""
首页:http://new.vip.weibo.cn/fourvipyear/room?from=1065093010&ua=iPhone7,
1__weibo__6.5.0__iphone__os9.3.1
抽微博会员红包
"""
from time import sleep
import sys
from re import findall
from requests import get
from pyquery import PyQuery as pyq
# pylint: disable=too-few-public-methods
class WeiBoHongBao:
"""参加微博四周年活动抽微博会员红包
"""
__cookie = {
'SUB': '你的微博Cookie里的SUB值',
}
__page = 1
__isover = False
def __str__(self):
return '抢微博会员红包!'
def __init__(self, _page=1):
self.__page = _page
def __lottery(self, uid):
"""抽奖
"""
try:
resp = get('http://new.vip.weibo.cn/fourvipyear/' +
'result?share=0&uid=' + uid,
cookies=self.__cookie,)
query = pyq(resp.text[1:])
money_num = query('.money_num').text()
nickname = query('.nickname').text()
# 领不了的就不再显示了
if money_num.find('来晚') < 0:
print('[{0}]{1}'.format(nickname, money_num))
# 若是今天领红包次数用完,就设置结束标志,并退出应用
self.__isover = money_num.find('用完') > 0
if self.__isover:
exit(0)
except TimeoutError:
print('TimeoutError')
except ConnectionError:
print('ConnectionError')
def gethongbaolist(self):
"""获得可用红包列表
"""
resp = get('http://new.vip.weibo.cn/aj/fourvipyear/' +
'gethongbaolist?page=' + str(self.__page),
cookies=self.__cookie,)
if len(resp.text) > 20 and resp.json()['msg'] == 'success':
while not self.__isover:
query = pyq(resp.json()['data']['html'][1:])
# 可领红包数
count = len(query('a')) - 1
for i in range(count):
if self.__isover:
break
uid = query('a').eq(i).attr('href')[48:]
nickname = query('h3.m-text-cut').eq(i).text()
tips = query('h4.m-text-cut').eq(i).text()
days = findall(r'([\d]+?)天微博会员', tips)
if len(days) > 0:
print('[ {} ]有{}天的会员券'.format(nickname, days[0]))
# 只领取微博会员天数在60天以上的红包
if len(days) > 0 and int(days[0]) >= 60:
self.__lottery(uid,)
# 若是没有更多的数据,就在一分钟后重试
if resp.json()['data']['more'] == 0:
self.__page = 1
print('没有找到合适1分钟后重试')
sleep(60)
else:
self.__page = int(self.__page) + 1
# 继续在下一页里查找
self.gethongbaolist()
else:
print(resp.text)
if __name__ == '__main__':
if (len(sys.argv)) > 1:
try:
WeiBoHongBao(sys.argv[1]).gethongbaolist()
except KeyboardInterrupt:
pass
else:
PAGE = input('请输入页码:')
WeiBoHongBao(PAGE).gethongbaolist()