![]() |
|
[Tut] I Used This Python Script to Check a Website Every X Seconds for a Given Word - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] I Used This Python Script to Check a Website Every X Seconds for a Given Word (/thread-100555.html) |
[Tut] I Used This Python Script to Check a Website Every X Seconds for a Given Word - xSicKxBot - 01-11-2023 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 Code: xThe Code ![]() I used the following code to accomplish this easily and quickly. You can copy&paste it, just make sure to adjust the highlighted Code: urlCode: wordCode: xCode: 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'Code Explanation ![]() This code snippet uses the Python Code: requestsYou use the Code: requestsCode: r Recommended: Python Request Library – Understanding Code: get()Then you check if the word appears in the response text using Code: r.text
Finally, the code pauses for two (or Code: xYou can stop the code by hitting Code: CTRL+C Recommended: How to Stop a Python Script?
|