上周末看了几位牛人用python写虾米网自动签到,其中还有一个利用sae平台上cron任务实现了自动签到。
于是也顺便申请了一下sae上的python内测,来实验了一下虾米的自动签到,已经成功签到了两天,呵呵……
以下是需要的代码,其中config.yaml是用来部署cron任务的。我现在是让它每天7:00自动执行自动签到,sae中有对应的cron日志可以检测是否执行成功。
01 #--coding:utf-8--
02 from bottle import Bottle, run
03
04 import sae
05 import urllib2
06 import urllib
07 import cookielib
08 import re
09
10
11 app = Bottle()
12
13 @app.route(‘/hello’)
14 def hello():
15 return “Hello, world! - Bottle”
16
17 @app.route(‘/xiamicheckin’ , method=‘GET’)
18
19 def xiami():
20 email = “xxxx@qq.com”
21 password = “**“
22
23 # Init
24 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookielib.CookieJar()))
25 urllib2.install_opener(opener)
26
27 # Login
28 login_url = ‘http://www.xiami.com/web/login‘
29 login_data = urllib.urlencode({‘email’:email, ‘password’:password, ‘LoginButton’:‘登陆’,})
30 login_headers = {‘Referer’:‘http://www.xiami.com/web/login‘, ‘User-Agent’:‘Opera/9.60’,}
31 login_request = urllib2.Request(login_url, login_data, login_headers)
32 login_response = urllib2.urlopen(login_request).read()
33
34 # Checkin
35 checkin_pattern = re.compile(r’<a class=”check_in” href=”(.*?)”>’)
36 checkin_result = checkin_pattern.search(login_response)
37
38 #return checkin_result.group(1)
39
40 checkin_url = ‘http://www.xiami.com‘ + checkin_result.group(1)
41 checkin_headers = {‘Referer’:‘http://www.xiami.com/web‘, ‘User-Agent’:‘Opera/9.60’,}
42 checkin_request = urllib2.Request(checkin_url, None, checkin_headers)
43 checkin_response = urllib2.urlopen(checkin_request).read()
44
45 return “whatever its done!”
46
47
48 application = sae.create_wsgi_app(app)
02 from bottle import Bottle, run
03
04 import sae
05 import urllib2
06 import urllib
07 import cookielib
08 import re
09
10
11 app = Bottle()
12
13 @app.route(‘/hello’)
14 def hello():
15 return “Hello, world! - Bottle”
16
17 @app.route(‘/xiamicheckin’ , method=‘GET’)
18
19 def xiami():
20 email = “xxxx@qq.com”
21 password = “**“
22
23 # Init
24 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookielib.CookieJar()))
25 urllib2.install_opener(opener)
26
27 # Login
28 login_url = ‘http://www.xiami.com/web/login‘
29 login_data = urllib.urlencode({‘email’:email, ‘password’:password, ‘LoginButton’:‘登陆’,})
30 login_headers = {‘Referer’:‘http://www.xiami.com/web/login‘, ‘User-Agent’:‘Opera/9.60’,}
31 login_request = urllib2.Request(login_url, login_data, login_headers)
32 login_response = urllib2.urlopen(login_request).read()
33
34 # Checkin
35 checkin_pattern = re.compile(r’<a class=”check_in” href=”(.*?)”>’)
36 checkin_result = checkin_pattern.search(login_response)
37
38 #return checkin_result.group(1)
39
40 checkin_url = ‘http://www.xiami.com‘ + checkin_result.group(1)
41 checkin_headers = {‘Referer’:‘http://www.xiami.com/web‘, ‘User-Agent’:‘Opera/9.60’,}
42 checkin_request = urllib2.Request(checkin_url, None, checkin_headers)
43 checkin_response = urllib2.urlopen(checkin_request).read()
44
45 return “whatever its done!”
46
47
48 application = sae.create_wsgi_app(app)
XML+Django/Jinja语言: config.yaml
01 —
02 name: cloudroc
03 version: 1
04 accesskey: l4x2m5jwzj
05 cron:
06 - description: xiami_auto_checkin
07 url: xiamicheckin
08 schedule: every day of month 07:00
09 timezone: Beijing
10 …
02 name: cloudroc
03 version: 1
04 accesskey: l4x2m5jwzj
05 cron:
06 - description: xiami_auto_checkin
07 url: xiamicheckin
08 schedule: every day of month 07:00
09 timezone: Beijing
10 …
初学python感觉代码很草根,不像java那么博大精深看半天还感觉莫名奇妙,python里面模拟http请求的类库无比强大,也难怪google里面很多搜索元件都用python实现。
就是python的缩进实在太头疼,开始config.yaml总是配置不成功,后来才发现是空格和tab都用了造成的,哎。