Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Website Blocker Using Python
Next article icon

Python script to monitor website changes

Last Updated : 03 Apr, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we are going to discuss how to create a python script to monitor website changes. You can code a program to monitor a website and it will notify you if there are any changes. This program has many useful scenarios for example if your school website has updated something you will come to know about it. 

Approach:

We will follow the following steps to write this program:

  1. Read the URL you want to monitor.
  2. Hash the entire website.
  3. Wait for a specified amount of seconds.
  4. If there are any changes as compared to the previous hash notify me else wait and again and then again take the hash.

Libraries required:

Libraries we will be using are:

  • time: To wait for a specified amount of time.
  • hashlib: To hash the content of the entire website.
  • urllib: To perform the get request and load the content of the website.

Implementation:

Python3
# Importing libraries import time import hashlib from urllib.request import urlopen, Request  # setting the URL you want to monitor url = Request('www.geeksforgeeks.org',               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") 

Output:

output

Note: time.sleep() takes seconds as a parameter. You can make changes to notifications instead of printing the status on the terminal you can write a program to get an email.

Code Explanation:

  1. The code starts by importing the libraries.
  2. Then it sets up a URL to monitor and performs a GET request on that website.
  3. The response is then stored in a variable called response.
  4. Next, the hash of the response is created with the help of hashlib and stored in currentHash.
  5. Next, time is set to sleep for 10 seconds before continuing while looping through an infinite loop which will continue until something changes or there's an exception.
  6. If anything changes, it will be printed out as well as another GET request performed on the website again after 30 seconds has passed without any change happening to either hashes (the first one or second one).
  7. The code is a Python script that monitors an URL for changes and notifies the user if there is one.
  8. The code first imports libraries needed to perform the desired task.
  9. It then sets up the URL to monitor, which will be www.geeksforgeeks.org Next, it performs a GET request on the website and stores it in a variable called response.
  10. The code then creates an initial hash using sha224(response).hexdigest().
  11. Next, it sleeps for 10 seconds before iterating through the loop again with urlopen(url).read() being performed every 30 seconds.
  12. If something changed in the hashes of currentHash and newHash, then print("something changed") is done to notify that something has changed.

Next Article
Website Blocker Using Python

H

hg070401
Improve
Article Tags :
  • Python
  • python-utility
Practice Tags :
  • python

Similar Reads

  • Python Script to Open a Web Browser
    In this article we will be discussing some of the methods that can be used to open a web browser (of our choice) and visit the URL we specified, using python scripts. In the Python package, we have a module named webbrowser, which includes a lot of methods that we can use to open the required URL in
    4 min read
  • How to Scrape Websites with Beautifulsoup and Python ?
    Have you ever wondered how much data is created on the internet every day, and what if you want to work with those data? Unfortunately, this data is not properly organized like some CSV or JSON file but fortunately, we can use web scraping to scrape the data from the internet and can use it accordin
    10 min read
  • Scrape Tables From any website using Python
    Scraping is a very essential skill for everyone to get data from any website. Scraping and parsing a table can be very tedious work if we use standard Beautiful soup parser to do so. Therefore, here we will be describing a library with the help of which any table can be scraped from any website easi
    3 min read
  • How to scrape the web with Playwright in Python
    In this article, we will discuss about Playwright framework, Its feature, the advantages of Playwright, and the Scraping of a basic webpage. The playwright is a framework for Web Testing and Automation. It is a fairly new web testing tool from Microsoft introduced to let users automate webpages more
    3 min read
  • Website Blocker Using Python
    This is real world program which blocks certain distracting website like Facebook, Youtube etc during your work hours. About the program : What we are going to in this program is that we will pass the link of websites which you think is distracting and the time that you are working on your computer
    3 min read
  • How to Check Loading Time of Website using Python
    In this article, we will discuss how we can check the website's loading time. Do you want to calculate how much time it will take to load your website? Then, what you must need to exactly do is subtract the time obtained passed since the epoch from the time obtained after reading the whole website.
    3 min read
  • How to Scrape Multiple Pages of a Website Using Python?
    Web Scraping is a method of extracting useful data from a website using computer programs without having to manually do it. This data can then be exported and categorically organized for various purposes. Some common places where Web Scraping finds its use are Market research & Analysis Websites
    6 min read
  • Python | Add Logging to a Python Script
    In this article, we will learn how to have scripts and simple programs to write diagnostic information to log files. Code #1 : Using the logging module to add logging to a simple program import logging def main(): # Configure the logging system logging.basicConfig(filename ='app.log', level = loggin
    2 min read
  • How to Run Python Script in GitHub Actions ?
    A tool available on GitHub that can help you automate chores in your software projects is called GitHub Actions. It enables you to design workflows that, when executed automatically, carry out actions like as deploying, testing, and even sending out notifications. It basically works like a small rob
    6 min read
  • How to Extract Script and CSS Files from Web Pages in Python ?
    Prerequisite: RequestsBeautifulSoupFile Handling in Python In this article, we will discuss how to extract Script and CSS Files from Web Pages using Python. For this, we will be downloading the CSS and JavaScript files that were attached to the source code of the website during its coding process. F
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences