Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] I Used This Python Script to Check a Website Every X Seconds for a Given Word
1
I Used This Python Script to Check a Website Every X Seconds for a Given Word

5/5 – (1 vote)

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

[Image: 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! [Image: 1f447.png]

[Image: 1f4ac.png] Project Challenge: How to write a Python script that reads content from a website every
Code:
x
seconds and checks if a specific word appears there?

The Code

[Image: 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
Code:
url
,
Code:
word
, and
Code:
x
variables to your need:

Code:
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
Code:
'finxter'
appears there. If you need to check another word on another URL, just change the variables accordingly.

Code Explanation

[Image: image-107-1024x768.png]

This code snippet uses the Python
Code:
requests
library to read from a website every two seconds and check if a certain word appears there.

You use the
Code:
requests
library to issue an HTTP request to the specified website and save the response in the variable
Code:
r
.

[Image: 1f449.png] Recommended: Python Request Library – Understanding
Code:
get()


Then you check if the word appears in the response text using
Code:
r.text
.
  • If it does, it prints the message
    Code:
    "The word appears on the website"
    and

  • If it does not, it prints the message
    Code:
    "The word does not appear on the website"
    .

Finally, the code pauses for two (or
Code:
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
Code:
CTRL+C
.

[Image: 1f449.png] Recommended: How to Stop a Python Script?
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: