class v2ex():
def __init__(self):
self.s = requests.Session()
self.headers = {
'Host':'
www.v2ex.com',
'Origin':'
http://www.v2ex.com',
'Referer':'
http://www.v2ex.com/signin',
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36'
}
def get_requires_data(self):
html = self.s.get('
http://www.v2ex.com/signin', headers=self.headers).text
once = re.search(r'value="(\d{5})"', html).group(1)
username = re.search(r'type="text" class="sl" name="(.*?)"', html).group(1)
password = re.search(r'type="password" class="sl" name="(.*?)"', html).group(1)
return {
'once': once,
'username': username,
'password': password
}
def login(self, username, password):
data = self.get_requires_data()
form_data = {
data['username']: username,
data['password']: password,
'once': data['once'],
'next': '/'
}
res =
self.s.post('
http://www.v2ex.com/signin', data=form_data, headers=self.headers)
return res
def sign(self):
html = self.s.get('
http://www.v2ex.com/mission/daily', headers=self.headers).text
once_code = re.search(r'signout\?once=(\d{5})', html).group(1)
url = '
http://www.v2ex.com/mission/daily/redeem?once=' + once_code
res = self.s.get(url, headers=self.headers)
result = res.text
days = re.search(r'已连续登录 (\d+) 天', result).group(1)
if not result.find('今天的登录奖励已经领取过了哦'):
# 如果找不到,则说明签到成功。
print('[v2ex]签到成功,已经连续签到%s 天'%days)
else:
# 如果找得到,则代表已经签到过了
print('[v2ex]签到失败,您已经签到过了,已经连续签到%s 天'%days)