Python實現(xiàn)周期性抓取網(wǎng)頁內(nèi)容的方法
本文實例講述了Python實現(xiàn)周期性抓取網(wǎng)頁內(nèi)容的方法。分享給大家供大家參考,具體如下:
1.使用sched模塊可以周期性地執(zhí)行指定函數(shù)
2.在周期性執(zhí)行指定函數(shù)中抓取指定網(wǎng)頁,并解析出想要的網(wǎng)頁內(nèi)容,代碼中是六維論壇的在線人數(shù)
論壇在線人數(shù)統(tǒng)計代碼:
#coding=utf-8 import time,sched,os,urllib2,re,string #初始化sched模塊的scheduler類 #第一個參數(shù)是一個可以返回時間戳的函數(shù),第二個參數(shù)可以在定時未到達之前阻塞。 s = sched.scheduler(time.time,time.sleep) #被周期性調(diào)度觸發(fā)的函數(shù) def event_func(): req = urllib2.Request('http://bt.neu6.edu.cn/') 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 # 保存結(jié)果,供圖表工具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() #enter四個參數(shù)分別為:間隔事件、優(yōu)先級(用于同時間到達的兩個事件同時執(zhí)行時定序)、被調(diào)用觸發(fā)的函數(shù),給他的參數(shù)(注意:一定要以tuple給如,如果只有一個參數(shù)就(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程序設(shè)計有所幫助。
- Python 抓取動態(tài)網(wǎng)頁內(nèi)容方案詳解
- 零基礎(chǔ)寫python爬蟲之使用urllib2組件抓取網(wǎng)頁內(nèi)容
- python抓取網(wǎng)頁內(nèi)容示例分享
- python抓取網(wǎng)頁中圖片并保存到本地
- Python3使用requests包抓取并保存網(wǎng)頁源碼的方法
- python抓取網(wǎng)頁圖片并放到指定文件夾
- 用Python程序抓取網(wǎng)頁的HTML信息的一個小實例
- Python實現(xiàn)抓取網(wǎng)頁并且解析的實例
- Python爬蟲實現(xiàn)網(wǎng)頁信息抓取功能示例【URL與正則模塊】
- Python簡單實現(xiàn)網(wǎng)頁內(nèi)容抓取功能示例
相關(guān)文章
python 使用openpyxl讀取excel數(shù)據(jù)
這篇文章主要介紹了python 使用openpyxl讀取excel數(shù)據(jù)的方法,幫助大家更好的理解和學習使用python,感興趣的朋友可以了解下2021-02-02淺析python打包工具distutils、setuptools
python包在開發(fā)中十分常見,一般的使用套路是所有的功能做一個python模塊包,打包模塊,然后發(fā)布,安裝使用。這篇文章給大家介紹了python打包工具distutils、setuptools的相關(guān)知識,感興趣的朋友一起看看吧2018-04-04Python中map,reduce,filter和sorted函數(shù)的使用方法
這篇文章主要介紹了Python中map,reduce,filter和sorted函數(shù)的使用方法,是Python入門學習中的基礎(chǔ)知識,需要的朋友可以參考下2015-08-08python自帶tkinter庫實現(xiàn)棋盤覆蓋圖形界面
這篇文章主要為大家詳細介紹了python自帶tkinter庫實現(xiàn)棋盤覆蓋圖形界面,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07