Create an account


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



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?



https://www.sickgaming.net/blog/2023/01/...iven-word/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Check Python Version from Command Line and in Script xSicKxBot 0 1,911 08-24-2023, 01:34 PM
Last Post: xSicKxBot
  [Tut] How to Integrate ChatGPT on Your Website Easily (Overview) xSicKxBot 0 1,510 05-18-2023, 01:20 AM
Last Post: xSicKxBot
  [Tut] How I Designed a Personal Portfolio Website with Django (Part 3) xSicKxBot 0 1,332 05-17-2023, 08:16 AM
Last Post: xSicKxBot
  [Tut] How I Designed a Personal Portfolio Website with Django (Part 2) xSicKxBot 0 1,303 05-03-2023, 12:30 PM
Last Post: xSicKxBot
  [Tut] Python to .exe – How to Make a Python Script Executable? xSicKxBot 0 1,265 03-13-2023, 01:31 AM
Last Post: xSicKxBot
  [Tut] EzpzShell: An Easy-Peasy Python Script That Simplifies Revshell Creation xSicKxBot 0 1,345 02-11-2023, 10:30 AM
Last Post: xSicKxBot
  [Tut] Bitcoin – Trading Moving Averages or HODL? A Python Script Uncovers the Answer! xSicKxBot 0 1,356 01-18-2023, 03:08 AM
Last Post: xSicKxBot
  [Tut] A Python Script to Uncover Creative Research Gaps Through Recombination xSicKxBot 0 1,222 01-04-2023, 07:58 AM
Last Post: xSicKxBot
  [Tut] Receive Automated Email Notification on Website Update xSicKxBot 0 1,254 12-23-2022, 07:57 PM
Last Post: xSicKxBot
  [Tut] I Made a Python Script That Beeps When BTC or ETH Prices Drop xSicKxBot 0 1,114 12-18-2022, 11:52 AM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016