签到领金币

  1 #!/use/bin/env python
  2 # -*- coding: utf-8 -*
  3 # Author    :    nick
  4 # Desc        :    v2ex每日签到
  5 
  6 import urllib
  7 import urllib2
  8 import cookielib
  9 import re
 10 import sys
 11 from bs4 import BeautifulSoup as bs 
 12 from datetime import datetime
 13 
 14 
 15 reload(sys)
 16 sys.setdefaultencoding('utf-8')
 17 
 18 
 19 
 20 
 21 login_URL = 'http://www.v2ex.com/signin'        #登录地址
 22 daily_URl = 'http://www.v2ex.com/mission/daily' #签到地址
 23 balance_url = 'http://v2ex.com/balance'            #账户余额
 24 user = 'test'                          #账号
 25 passwd = 'test'
 26 today = datetime.today()
 27 time = today.strftime("%Y-%m-%d %X ")
 28 
 29 '''
 30 由于需要登录后访问页面,所以这里需要使用cookie进行session跟踪
 31 '''
 32 
 33 cookie = cookielib.CookieJar()                        #获取一个cookie对象
 34 handler = urllib2.HTTPCookieProcessor(cookie)        #利用urllib2库的HTTPCookieProcessor对象来创建cookie处理器
 35 opener = urllib2.build_opener(handler)                #装载cookie,通过handler来构建opener
 36 
 37 '''
 38 opener:当你获取一个URL你使用一个opener(一个urllib2.OpenerDirector的实例)。平时使用的默认opener就是urlopen,他是一个特殊的opener,
 39 可以理解成opener的一个特殊实例,传入的参数仅仅是url,data,timeout。
 40 '''
 41 
 42 
 43 def get_value(html,name,tag):
 44     '''
 45     获取once的值,每次都不一样
 46     '''
 47     soup = bs(html, 'html.parser')
 48     if name:
 49         value = soup.find(attrs={tag:name})
 50     else:
 51         value =soup.find(tag)
 52     return value
 53 
 54 
 55 def login():
 56     '''
 57     登录
 58     '''
 59     login_req = urllib2.Request(login_URL)
 60     html = opener.open(login_req).read()
 61     once_value = get_value(html,'once','name')['value']
 62 
 63     header ={
 64         'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.94 Safari/537.36',
 65         "Host" : "www.v2ex.com",
 66         "Origin" : "http://www.v2ex.com",
 67         'Referer':'http://www.v2ex.com/signin'
 68     }
 69 
 70     post_info = {'u':user,'p':passwd,'once':once_value,'next':'/'}
 71     postdata = urllib.urlencode(post_info)
 72     req = urllib2.Request(login_URL,postdata,header)
 73     response = opener.open(req,timeout=10)
 74 #    print response.getcode()
 75 #    print response.read()
 76 
 77     if not 'A2' in [c.name for c in cookie]:
 78         raise ValueError,"登录失败!"
 79 
 80 
 81 def daily():
 82     '''
 83     每日登录奖励 
 84     '''
 85     daily_req = urllib2.Request(daily_URl)
 86     daily_page = opener.open(daily_req).read()
 87     day = re.findall(r"已连续登录\s?(\d+)\s?天",daily_page)[0]
 88     info = get_value(daily_page,'fa fa-ok-sign','class')
 89     if info:
 90         print "%s每日登录奖励已经领取,已连续登录%s天" %(time,day)
 91     else:
 92         print "%s开始登录领取奖励....."% time
 93         onclick = get_value(daily_page,'super normal button','class')['onclick']
 94         href = onclick[onclick.find("'")+1:len(onclick)-2]
 95         full_url = 'http://www.v2ex.com'+href
 96         full_req = urllib2.Request(full_url)
 97         page = opener.open(full_req).read()
 98         success = get_value(daily_page,'icon-ok-sign','class')
 99         if success:
100             day = int(day)
101             day +=1
102             print "%s签到成功!已成功领取每日奖励,已连续登录 %d 天" % (time,day)
103             sys.exit(0)
104         else:
105             print "%s签到失败!" % time
106 
107 def balance():
108     '''
109     账户余额
110 
111     '''
112     balance_req = urllib2.Request(balance_url)
113     balance_page = opener.open(balance_req).read()
114     soup = bs(html, 'html.parser')
115 
116 
117 
118 
119 
120 if __name__ == '__main__':
121     login()
122     daily()
View Code

然后丢到服务器上,因为v2ex每天早上8点签到时间重置,所以把crontab设置到八点十分。然后就等着收金币吧。O(∩_∩)O~~

10 8 * * *  sudo python /mnt/v2ex.py >>/mnt/v2ex.log

 

posted @ 2015-07-05 23:34  亡灵法  阅读(556)  评论(0编辑  收藏  举报