I Used This Python Script to Check a Website Every X Seconds for a Given Word : Chris
by: Chris
blow post content copied from Finxter
click here to view original post
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
![](https://blog.finxter.com/wp-content/uploads/2023/01/image-109.png)
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
![](https://blog.finxter.com/wp-content/uploads/2023/01/check_website_word.gif)
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
![](https://blog.finxter.com/wp-content/uploads/2023/01/image-107-1024x768.png)
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?
January 10, 2023 at 10:37PM
Click here for more details...
=============================
The original post is available in Finxter by Chris
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================
![Salesforce Salesforce](https://1.bp.blogspot.com/-7PAEudGm-vA/X5xmpBCB6-I/AAAAAAAAACU/4l5vI_EgYqYlEXFHs1xEeyiYP9Dyl89IgCLcBGAsYHQ/s600/1.jpg)
Post a Comment