Sick Gaming
[Tut] I Used This Python Script to Check a Website Every X Seconds for a Given Word - Printable Version

+- Sick Gaming (https://www.sickgaming.net)
+-- Forum: Programming (https://www.sickgaming.net/forum-76.html)
+--- Forum: Python (https://www.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

<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;1048684&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 142.5px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div>
</p></div>
<p>I host multiple websites such as </p>
<ul>
<li><a rel="noreferrer noopener" href="https://finxter.com" target="_blank">https://finxter.com</a>, </li>
<li><a rel="noreferrer noopener" href="https://blog.finxter.com" target="_blank">https://blog.finxter.com</a>, and </li>
<li><a href="https://pythononeliners.com">https://pythononeliners.com</a>. </li>
</ul>
<p>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). </p>
<h2>The Project</h2>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="586" height="880" src="https://blog.finxter.com/wp-content/uploads/2023/01/image-109.png" alt="" class="wp-image-1048727" srcset="https://blog.finxter.com/wp-content/uploads/2023/01/image-109.png 586w, https://blog.finxter.com/wp-content/uploads/2023/01/image-109-200x300.png 200w" sizes="(max-width: 586px) 100vw, 586px" /></figure>
</div>
<p>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. </p>
<p>Because I thought many people will probably have the same problem, I’ll share this code here. So, let’s get started! <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4ac.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Project Challenge</strong>: How to write a Python script that reads content from a website every <code>x</code> seconds and checks if a specific word appears there?</p>
<h2>The Code</h2>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" loading="lazy" width="790" height="569" src="https://blog.finxter.com/wp-content/uploads/2023/01/check_website_word.gif" alt="" class="wp-image-1048710"/></figure>
</div>
<p>I used the following code to accomplish this easily and quickly. You can copy&amp;paste it, just make sure to adjust the highlighted <code>url</code>, <code>word</code>, and <code>x</code> variables to your need:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="5-7" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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)
</pre>
<p>In this case, I check the Wikipedia page on Graph Partitioning if the keyword <code>'finxter'</code> appears there. If you need to check another word on another URL, just change the variables accordingly. </p>
<h2>Code Explanation</h2>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" loading="lazy" width="1024" height="768" src="https://blog.finxter.com/wp-content/uploads/2023/01/image-107-1024x768.png" alt="" class="wp-image-1048724" srcset="https://blog.finxter.com/wp-content/uploads/2023/01/image-107-1024x768.png 1024w, https://blog.finxter.com/wp-content/uploads/2023/01/image-107-300x225.png 300w, https://blog.finxter.com/wp-content/uploads/2023/01/image-107-768x576.png 768w, https://blog.finxter.com/wp-content/uploads/2023/01/image-107.png 1174w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p>This code snippet uses the Python <code><a href="https://blog.finxter.com/python-requests-library/" data-type="post" data-id="37796" target="_blank" rel="noreferrer noopener">requests</a></code> library to read from a website every two seconds and check if a certain word appears there. </p>
<p>You use the <code>requests</code> library to issue an HTTP request to the specified website and save the response in the variable <code>r</code>.</p>
<p class="has-base-background-color has-background"> <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended</strong>: <a rel="noreferrer noopener" href="https://blog.finxter.com/python-requests-get-the-ultimate-guide/" data-type="post" data-id="37837" target="_blank">Python Request Library – Understanding <code>get()</code></a></p>
<p>Then you check if the word appears in the response text using <code>r.text</code>.</p>
<ul>
<li>If it does, it <a rel="noreferrer noopener" href="https://blog.finxter.com/python-print/" data-type="post" data-id="20731" target="_blank">prints</a> the message <code>"The word appears on the website"</code> and </li>
<li>If it does not, it prints the message <code>"The word does not appear on the website"</code>. </li>
</ul>
<p>Finally, the code pauses for two (or <code>x</code>!) seconds before making another request. This process is repeated continuously to ensure that the website is checked regularly.</p>
<p>You can stop the code by hitting <code>CTRL+C</code>. </p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended</strong>: <a href="https://blog.finxter.com/how-to-stop-a-python-script-keyboard-and-programmatically/" data-type="post" data-id="23541" target="_blank" rel="noreferrer noopener">How to Stop a Python Script?</a></p>
</div>


https://www.sickgaming.net/blog/2023/01/10/i-used-this-python-script-to-check-a-website-every-x-seconds-for-a-given-word/