前一段时间买了个域名,绑定到github的静态博客上,也就相当于一个独立博客了。 自己可以添加各种站长统计,但是回过头来到CSDN这个博客却没法查看某段时间的访问统计,只有一个总的访问量。于是我就想自己弄个定时任务,抓取页面上这个总的访问次数,然后存到数据库中,然后通过js画图插件来查看一段时间的访问变化以及每个时间段的访问情况。
正好前一段时间申请了一个新浪sae的账号一直没有怎么用,于是就决定写几个脚本来实现这个功能
环境: SAE
软件: python webby,bootstrap,hightcharts
使用到的服务: mysql cron定时任务
描述:
定时任务: 每小时的0分钟开始抓取csdn博客访问次数,把访问总量和本次访问量与上次访问的差值 插入到mysq中
页面访问: 通过js画图软件画出整个访问趋势
主要的代码:
sae cron配置 config.yaml
name: loveshijian version: 1 libraries: - name: webppy version: "0.36" - name: lxml version: "2.3.4" cron: - description: cron test url: cron/show schedule: "0 * * * *"
抓取方法: weixin_handler.py
def get_click(): ''' get my blog clicked number ''' user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' headers = { 'User-Agent' : user_agent } request = urllib2.Request(BLOG_URL, headers=headers) res = urllib2.urlopen(request) the_page = res.read() #print the_page res = '' pattern = re.compile('''<li>访问:<span>(.*)</span></li>''', re.M) info_str = pattern.findall(the_page) if len(info_str)>0: res = info_str[0] else: pattern = re.compile('''<LI>访问:<SPAN>(.*)</SPAN></LI>''', re.M) info_str = pattern.findall(the_page) res = info_str[0] return res
原理很简单,一个下午也就实现了,但是highcharts用的不是很好,时间轴显示一直有问题。
源码地址: https://github.com/orangle/SAEcode
展示页面: http://loveshijian.sinaapp.com/hour_show
有时间继续重构,webpy之前没有使用过,都是现用现查,代码组织结构比较混乱。。
本文出自 “orangleliu笔记本” 博客,请务必保留此出处http://blog.csdn.net/orangleliu/article/details/39184291