python网页数据抓取( ,涉及Python时间函数及正则匹配的相关操作技巧)
优采云 发布时间: 2021-09-14 14:08python网页数据抓取(
,涉及Python时间函数及正则匹配的相关操作技巧)
Python实现周期性抓取网页内容的方法
更新时间:2015-11-04 15:22:07 作者:intergret
这个文章主要介绍了Python中定时抓取网页内容的方法,涉及到Python时间函数和正则匹配的相关操作技巧。有一定的参考价值,有需要的朋友可以参考以下
本文介绍了 Python 如何实现定期抓取网页内容的示例。分享给大家,供大家参考,如下:
1.使用sched模块定期执行指定的函数
2.在指定函数的周期性执行中抓取指定网页,解析出想要的网页内容,代码为六味论坛在线人数
用于统计论坛在线人数的代码:
<p>
#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.compile(r'总计 .*? 人在线')
usernummatch = usernump.findall(rawdata)
if usernummatch:
currentnum=usernummatch[0]
currentnum=currentnum[string.index(currentnum,'>')+1:string.rindex(currentnum,'