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).
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?
I used the following code to accomplish this easily and quickly. You can copy&paste it, just make sure to adjust the highlighted
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.
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
👉 Recommended: Python Request Library – Understanding
Then you check if the word appears in the response text using
- 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
👉 Recommended: How to Stop a Python Script?
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.