Home>

1. Use the sched module to periodically execute the specified function

2. Crawl the specified webpage in the periodic execution of the specified function,And parse out the desired web content,In the code is the number of online users of the six-dimensional forum

Forum online statistics code:

#coding=utf-8
import time, sched, os, urllib2, re, string
#Initial scheduler class of the schedule module
#The first parameter is a function that can return a timestamp,The second parameter can be blocked before the timing arrives.
s=sched.scheduler (time.time, time.sleep)
#Functions triggered by periodic scheduling
def event_func ():
  req=urllib2.request ("http://bt.neu6.edu.cn/")
  response=urllib2.urlopen (req)
  rawdata=response.read ()
  response.close ()
  usernump=re.compile (r "Total<em>. *?</em>people online")
  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
    #Save results,For charting tool 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 ()
The four parameters of #enter are:interval event, priority (for two events that arrive at the same time and are sequenced at the same time), the function triggered by the call,Give him the parameters (note:must be given as a tuple, if there is only one parameter (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 ()
  • Previous Detailed explanation of singletast activation mode of Activity and how to use intent to pass values
  • Next PHP method to achieve static HTML page