用python sched模块周期抓取论坛在线人线示例,pythonsched,用python sche
用python sched模块周期抓取论坛在线人线示例,pythonsched,用python sche
用python sched模块周期抓取论坛在线人线示例。我的这段代码中使用了sched模块方法,它可以周期性地执行指定函数。并在指定函数中抓取指定网页和提取内容。这次的示例是抓取某论坛的在线人数。
#coding=utf-8import time,sched,os,urllib2,re,string#初始化sched模块的scheduler类#第一个参数是一个可以返回时间戳的函数,第二个参数可以在定时未到达之前阻塞。s = sched.scheduler(time.time,time.sleep)#被周期性调度触发的函数def event_func():req = urllib2.Request('XXX示例论坛地址')response = urllib2.urlopen(req)rawdata = response.read()response.close()usernump = re.compile(r'总计 <em>.*?</em> 人在线')usernummatch = usernump.findall(rawdata)if usernummatch:currentnum=usernummatch[0]currentnum=currentnum[string.index(currentnum,'>')+1:string.rindex(currentnum,'<')]print "Current Time:",time.strftime('%Y,%m,%d,%H,%M',time.localtime(time.time())),'User num:',currentnum# 保存结果,供图表工具amcharts使用result=open('liuvUserNUm','a')result.write('{year: new Date('+time.strftime('%Y,%m,%d,%H,%M',time.localtime(time.time()))+'),value:'+currentnum+'},\n')result.close()#www.iplaypy.com#enter四个参数分别为:间隔事件、优先级(用于同时间到达的两个事件同时执行时定序)、被调用触发的函数,给他的参数(注意:一定要以tuple给如,如果只有一个参数就(xx,))def perform(inc): s.enter(inc,0,perform,(inc,)) event_func() def mymain(inc=900): s.enter(0,0,perform,(inc,)) s.run()if __name__ == "__main__":mymain()
编橙之家文章,
相关内容
- Python方法调用网站接口实现MD5值查询,pythonmd5,这是在
- Python对多线程数量的限制方法,python多线程数量,当你在
- 一个简易的hash加密小工具源码,hash加密源码,一直喜欢
- 计算前一个月最后一天日期的python方法,最后一天pyt
- python计算上个月第一天日期的方法,python上个月,pytho
- 计算上个月有多少天的python简单小程序,上个月python
- django实现获取用户输入方法,django实现获取用户,这是用
- python自动登录并备份文件操作,python自动登录备份,py
- Python发可以带附件的email操作方法分享,pythonemail,Pyth
- python面向过程的简单通讯录示例,python通讯录示例,初学
评论关闭