v2ex签到脚本,结合crontab实现自动签到,免去手动签到麻烦,代码属于Demo级别,贴出来看看就好。
# encoding:utf-8
import urllib, urllib2, cookielib, re
# 账号相关参数
username = 'your username'
password = 'your password'
# cookie设置
cj = cookielib.CookieJar()
cookie_hanler = urllib2.HTTPCookieProcessor(cj)
# 获取表单
lgurl = 'http://v2ex.com/signin'
req = urllib2.Request(url = lgurl)
opener = urllib2.build_opener(cookie_hanler)
urllib2.install_opener(opener)
contents = opener.open(req).read()
# 根据正则表达式匹配once值
reg = r'value="(.*)" name="once"'
pattern = re.compile(reg)
result = pattern.findall(contents)
reg = r'class="sl" name=\"(.+)\" value'
pattern = re.compile(reg)
user_info = pattern.findall(contents)
# 登录参数设置
lgurl = 'http://v2ex.com/signin'
once = result[0]
data = {user_info[0]: username, user_info[1]: password, 'once': once, 'next': '/'}
data = urllib.urlencode(data)
hdr = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)'
' Chrome/40.0.2214.94 Safari/537.36',
'Referer': 'http://v2ex.com/signin',
'Host': 'v2ex.com'}
req = urllib2.Request(url = lgurl, data = data, headers = hdr)
opener = urllib2.build_opener(cookie_hanler)
# 进行登录操作
response = opener.open(req)
page = response.read()
# 领取铜币
contents = urllib2.urlopen('https://v2ex.com/mission/daily').read()
reg = r'onclick=\"location\.href = \'\/mission\/daily\/redeem\?once=(\d+)\';\"'
pattern = re.compile(reg)
random_num = pattern.findall(contents)[0]
get_money_url = 'https://v2ex.com/mission/daily/redeem?once=%s' % str(random_num)
result = urllib2.urlopen(get_money_url).read()