使用SAEPython在虾米网自动签到

丑陋的代码,请谅解。对于我来说糙快猛是第一位的

使用了新浪SAE下的python环境。其中内嵌了bottle框架。然后还用了cron定时任务服务。

使用了http://huxuan.org的自动签到代码,非常感谢!

页面代码,单文件:

index.wsgi

# -*- coding:utf-8 -*- 
from bottle import Bottle, run

import sae
import urllib2
import urllib
import cookielib
import re

app = Bottle()


@app.route('/')
def hello():
    return "Hello, world! - Bottle"
@app.route('/xiamicheckin' , method='GET')
def xiami():
   
    email = "xxxxx@qq.com"#请填写账号
    password = "1234567"#请填写密码
    # Init
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookielib.CookieJar()))
    urllib2.install_opener(opener)
    # Login
    login_url = 'http://www.xiami.com/web/login'
    login_data = urllib.urlencode({'email':email, 'password':password, 'LoginButton':'登陆',})
    login_headers = {'Referer':'http://www.xiami.com/web/login', 'User-Agent':'Opera/9.60',}
    login_request = urllib2.Request(login_url, login_data, login_headers)
    login_response = urllib2.urlopen(login_request).read()
    
    # Checkin
    checkin_pattern = re.compile(r'<a class="check_in" href="(.*?)">')
    checkin_result = checkin_pattern.search(login_response)
    checkin_url = 'http://www.xiami.com' + checkin_result.group(1)    
    checkin_headers = {'Referer':'http://www.xiami.com/web', 'User-Agent':'Opera/9.60',}
    checkin_request = urllib2.Request(checkin_url, None, checkin_headers)
    checkin_response = urllib2.urlopen(checkin_request).read()
    #return checkin_response
    return "done"
application = sae.create_wsgi_app(app)

config.yaml文件(配置cron定时任务)

---
name: yourappid
version: 1
cron:
  - description: xiami
    url: xiamicheckin
    schedule: 21 7 * * *

...

一些解释和注意:

1.SAE的 cron使用get方式触发任务。所以被触发的页面应接受get方式。

@app.route('/xiamicheckin' , method='GET')

2.bottle中,如果未指明method,则默认为get。

@app.route('/')

3.SAE的cron文档有问题,其中示例代码:

  #该BUG已被修复

name: crontest 
version: 1 
cron: 
- description: cron_test 
url: /cron/make 
schedule: "*/5 * * * *" 

应改为

name: crontest 
version: 1 
cron: 
- description: cron_test 
url: /cron/make 
schedule:  */5 * * * *

 

原文地址:https://www.cnblogs.com/bitspace/p/2521545.html