Wednesday, 15 April 2015

python - Get webpage data every 5 seconds and run a given times -



python - Get webpage data every 5 seconds and run a given times -

i want source code of web page every 5 seconds, , want multiple times. such 3 times, total time 3*5 = 15 seconds. wrote next code:

import urllib2 import threading def getwebsource(url): usock = urllib2.urlopen(url) info = usock.read() usock.close() open("webdata.txt", "a") myfile: myfile.write(data) myfile.write("\n\n") url = 'http://www.google.com/' n = 3 while n>0: t = threading.timer(5.0, getwebsource,args = [url]) # set seconds here t.start() n = n-1

however, when run it, is: run 5 seconds , read webpage 3 times. wrong it? expect should read webpage every 5 seconds , repeat 3 times.

#

update:thanks @wckd , here final code:

import urllib2 import time time import gmtime, strftime def getwebsource(url,fout,seconds,times): while times > 0: usock = urllib2.urlopen(url) info = usock.read() usock.close() currenttime = strftime("%y-%m-%d %h:%m:%s", gmtime()) fout.write(currenttime +"\n") fout.write(data) fout.write("\n\n") times = times - 1 time.sleep(seconds) url = 'http://www.google.com' fout = open("webdata.txt", "a") seconds = 5 times = 4 getwebsource(url,fout,seconds,times)

the method threading.timer() creates thread starts after designated amount of time. while thread waiting run loop continues run. have 3 threads run after 5 seconds.

if want have spacing create getwebsource recursive function countdown kick off new thread when gets run. or if want maintain doing doing multiply 5 n spacing. don't recommend because if seek 100 times you'll have 100 threads.

update

the easiest way in 1 thread add together wait phone call (also known sleep) loop

while n > 0 time.sleep(5) yourmethodhere() end

however method take time run maintain thread creation in there , set wait 0 seconds.

while n > 0 time.sleep(5) threading.timer(0, yourmethodhere()) n = n - 1 end

this way won't confined bad connection or slowing up.

python web timer

No comments:

Post a Comment