1.使用sched模块可以周期性地执行指定函数
2.在周期性执行指定函数中抓取指定网页,并解析出想要的网页内容,代码中是六维论坛的在线人数
论坛在线人数统计代码:
#coding=utf-8 import time,sched,os,urllib2,re,string #初始化sched模块的scheduler类 #第一个参数是一个可以返回时间戳的函数,第二个参数可以在定时未到达之前阻塞。 s = sched.scheduler(time.time,time.sleep) #被周期性调度触发的函数 def event_func(): req = urllib2.Request('http://bt.neu6.edu.cn/') response = urllib2.urlopen(req) rawdata = response.read() response.close() usernump = re测试数据pile(r'总计 .*? 人在线') usernummatch = usernump.findall(rawdata) if usernummatch: currentnum=usernummatch[0] currentnum=currentnum[string.index(currentnum,'>')+1:string.rindex(currentnum,'
希望本文所述对大家Python程序设计有所帮助。
查看更多关于Python实现周期性抓取网页内容的方法的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did89278