- 每分钟抓取一次课程页面学习人数.
- 和前次进行对比, 数量增加意味课程卖出, 播放本地音乐.
- 同时监听多们课程.
from threading import Timer
from scrapy.selector import Selector
from subprocess import getoutput
import requests
COURSES = {
# Python高级编程技巧实战
62: {
'url': 'http://coding.imooc.com/class/62.html',
'mp3': 'happy.mp3',
},
75: {
'url': 'http://coding.imooc.com/class/75.html',
'mp3': 'happy2.mp3',
},
74: {
'url': 'http://coding.imooc.com/class/74.html',
'mp3': 'happy3.mp3',
},
}
def getSalesVolume(courses):
res = {}
for course in courses:
url = COURSES[course]['url']
response = requests.get(url)
sel = Selector(text=response.text)
sv = sel.css('div.static-item:nth-child(3) > \
span.meta-value > strong::text').extract_first()
res[course] = int(sv)
return res
def loop(courses, svOld):
try:
sv = getSalesVolume(courses)
for course in courses:
print(course, svOld[course], sv[course])
cmd = 'play %s' % COURSES[course]['mp3']
for _ in range(sv[course] - svOld[course]):
getoutput(cmd)
svOld = sv
except IOError as e:
print(e)
Timer(60, loop, args=(courses, svOld)).start()
courses = [62, 75, 74]
loop(courses, getSalesVolume(courses))
打开App,阅读手记
热门评论
老师有python3的课程么
老师真会玩。。。。。。
看了源码,Timer是继承Thread,那不是每分钟都要新开一个线程来执行?这样的话感觉不如来个while 1配合time.sleep(60)。