使用SAE-Python在虾米网自动签到

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

使用了新浪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/archive/2012/05/28/2521545.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值