Python 3 Script to Monitor Website URL Changes – Website Monitoring Full Python Script for Beginners

Python 3 Script to Monitor Website URL Changes – Website Monitoring Full Python Script for Beginners

 

Welcome folks today in this blog post we will be monitoring website changes and this python script will update you whenever the content of the website has been changed. All the source code of the application is shown below.

 

 

 

Libraries Used

 

 

 

time library is used for waiting or adding delay in python application

 

hashlib It is a built in python library for hashing the content of the entire website

 

urllib It is the website to request the website url and load the website content

 

 

 

 

Python Script for Website URL Monitoring

 

 

 

 

Now make a python script app.py and copy paste the following code

 

 

app.py

 

 

 

# Importing libraries 
import time 
import hashlib 
from urllib.request import urlopen, Request 

# setting the URL you want to monitor 
url = Request('https://leetcode.com/', 
            headers={'User-Agent': 'Mozilla/5.0'}) 

# to perform a GET request and load the 
# content of the website and store it in a var 
response = urlopen(url).read() 

# to create the initial hash 
currentHash = hashlib.sha224(response).hexdigest() 
print("running") 
time.sleep(10) 
while True: 
    try: 
        # perform the get request and store it in a var 
        response = urlopen(url).read() 
        
        # create a hash 
        currentHash = hashlib.sha224(response).hexdigest() 
        
        # wait for 30 seconds 
        time.sleep(30) 
        
        # perform the get request 
        response = urlopen(url).read() 
        
        # create a new hash 
        newHash = hashlib.sha224(response).hexdigest() 

        # check if new hash is same as the previous hash 
        if newHash == currentHash: 
            continue

        # if something changed in the hashes 
        else: 
            # notify 
            print("something changed") 

            # again read the website 
            response = urlopen(url).read() 

            # create a hash 
            currentHash = hashlib.sha224(response).hexdigest() 

            # wait for 30 seconds 
            time.sleep(30) 
            continue
            
    # To handle exceptions 
    except Exception as e: 
        print("error")

 

READ  Python 3 Pandas Library Script to Merge Multiple XLSX Excel Files Full Project For Beginners

 

If you run the app.py file you will see the following screenshot

 

 

 

 

Leave a Reply