Tuesday 30 May 2017

Python script to detect changes in a webpage

Hi friends,

In this post, we'll see a python script to detect changes (if any) in a URL webpage. Results are around the corner and the anxiety among the students make them refresh the results page every second to see if the results have been declared or not. So, this post helps them to seat calmly and let the python script notify about the results. The python script keeps checking the results URL and play a beep sound if there are any changes in the URL. The same script can also be used to know about changes in the e-commerce websites like Flipkart, Amazon, etc. during the sale period to know about great offers.

from urllib.request import urlopen
import time
import os
import winsound

url = "http://upresults.nic.in/"  #results url
oldPage = urlopen(url).read()
updatedPage = oldPage 
counter = 0
flag = True

#while there are no new changes to the page
while oldPage == updatedPage:
 if flag:
  time.sleep(10)  #refresh every 10 seconds
 try:
  updatedPage = urlopen(url).read()
 except IOError:
  print("Error in reading url")
  flag = False
  continue 
 counter = counter + 1
 print(str(counter) + " times refreshed")
 flag = True

#play a beep sound
winsound.Beep(300, 2000)
Share:

0 comments:

Post a Comment