数据获取
本篇将记录如何使用python查询小猪巴士是否有新票放出,在ubuntu下使用fiddler对Android应用进行抓包
已经记录了如何真机抓包,线路查询接口如下图所示:
使用curl尝试发送请求:1
curl -X POST -d "lineId=150716105019140882&lineBaseId=150716105018892878" http://bsapp.pig84.com/customline_app/app_book/getLineAndClassInfo.action
1 | { |
其中list
为站点信息,list1
为预售日期信息,预售期内的日期有返回price
,所以将list1
反转,
找到第一次出现price
的条目并保存,再次请求时取出数据与前一次相比较,若相同则无新票预售。
代码如下:
check_bus.py1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43#!/usr/bin/env python
# -*- coding:utf-8 -*-
import requests
import json
import memcache
import datetime
# connect to memcached
mc = memcache.Client(['localhost:11211'], debug=0)
class BusBookChecker():
def __init__(self, lineId, lineBaseId):
self.url = 'http://bsapp.pig84.com/customline_app/app_book/getLineAndClassInfo.action'
self.payload = {"lineId": lineId, "lineBaseId": lineBaseId}
self.bookable_date = mc.get('bookable_date')
if not self.bookable_date:
bookable_date = datetime.date.today().strftime("%Y-%m-%d")
mc.set('bookable_date', bookable_date)
self.bookable_date = bookable_date
def check(self):
result = requests.post(self.url, self.payload)
order_dates = result.json()['list1']
order_dates.reverse()
for date in order_dates:
if 'price' in date:
if self.bookable_date != date['orderDate']:
mc.set('bookable_date', date['orderDate'])
send_message()
break
return self.bookable_date
def send_message():
print 'send sms to user'
if __name__ == "__main__":
checker = BusBookChecker(
'150716105019140882', '150716105018892878')
print u'当前预售期到' + checker.check()
修改check_bus.py的权限,增加可执行权限:1
2
3$ chmod u+x check_bus.py
$ ./check_bus.py
当前预售期到2016-01-15
定时执行
使用crontab每小时执行一次脚本,编辑任务列表1
$ crontab -e
在文件末尾添加一行,每小时执行一次查询。1
0 * * * * /path/check_bus.py
查看当前有哪些定时任务:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24$ crontab -l
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
0 * * * * /path/check_bus.py
删除所有定时任务,不提示是否确认:1
2
3$ crontab -r
$ crontab -l
no crontab for ming
删除任务时提示是否确认:1
2
3
4$ crontab -ir
crontab: really delete ming's crontab? (y/n) y
$ crontab -l
no crontab for ming
参考链接:
python 链接和操作 memcache
Ubuntu中memcached的安装、配置和启用关闭
每天一个linux命令(50):crontab命令