I host multiple websites such as
Many of them use caching services to speed up the loading time for visitors worldwide. Consequently, when I do change a website, the change does often not appear immediately on the site (because the stale cached site is served up).
The Project

To ensure that my website is up-to-date, I wrote a simple Python script that helps me regularly check my website every few seconds to see if a certain word appears there.
Because I thought many people will probably have the same problem, I’ll share this code here. So, let’s get started! π
π¬ Project Challenge: How to write a Python script that reads content from a website every x
seconds and checks if a specific word appears there?
The Code

I used the following code to accomplish this easily and quickly. You can copy&paste it, just make sure to adjust the highlighted url
, word
, and x
variables to your need:
import requests import time url = 'https://en.wikipedia.org/wiki/Graph_partition' word = 'finxter' x = 60 # seconds between two requests # Repeat forever while True: # Get the content from the website r = requests.get(url) # Check if word is on website if word in r.text: print("The word appears on the website") else: print("The word does not appear on the website") # Do nothing for some time (e.g., 60 seconds) to avoid spam requests time.sleep(x)
In this case, I check the Wikipedia page on Graph Partitioning if the keyword 'finxter'
appears there. If you need to check another word on another URL, just change the variables accordingly.
Code Explanation

This code snippet uses the Python requests
library to read from a website every two seconds and check if a certain word appears there.
You use the requests
library to issue an HTTP request to the specified website and save the response in the variable r
.
π Recommended: Python Request Library – Understanding get()
Then you check if the word appears in the response text using r.text
.
- If it does, it prints the message
"The word appears on the website"
and - If it does not, it prints the message
"The word does not appear on the website"
.
Finally, the code pauses for two (or x
!) seconds before making another request. This process is repeated continuously to ensure that the website is checked regularly.
You can stop the code by hitting CTRL+C
.
π Recommended: How to Stop a Python Script?