php 网页内容抓取(Python时间函数及正则匹配的相关操作技巧(一))
优采云 发布时间: 2021-09-30 00:35php 网页内容抓取(Python时间函数及正则匹配的相关操作技巧(一))
阅读:5193
本文文章主要介绍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'总计 .*&#63; 人在线') usernummatch = usernump.findall(rawdata) if usernummatch: currentnum=usernummatch[0] currentnum=currentnum[string.index(currentnum,'>')+1:string.rindex(currentnum,'