{"id":131104,"date":"2023-01-10T17:07:28","date_gmt":"2023-01-10T17:07:28","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=1048684"},"modified":"2023-01-10T17:07:28","modified_gmt":"2023-01-10T17:07:28","slug":"i-used-this-python-script-to-check-a-website-every-x-seconds-for-a-given-word","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2023\/01\/10\/i-used-this-python-script-to-check-a-website-every-x-seconds-for-a-given-word\/","title":{"rendered":"I Used This Python Script to Check a Website Every X Seconds for a Given Word"},"content":{"rendered":"\n<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;}'>\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/p><\/div>\n<p>I host multiple websites such as <\/p>\n<ul>\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/finxter.com\" target=\"_blank\">https:\/\/finxter.com<\/a>, <\/li>\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\" target=\"_blank\">https:\/\/blog.finxter.com<\/a>, and <\/li>\n<li><a href=\"https:\/\/pythononeliners.com\">https:\/\/pythononeliners.com<\/a>. <\/li>\n<\/ul>\n<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>\n<h2>The Project<\/h2>\n<div class=\"wp-block-image\">\n<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=\"auto, (max-width: 586px) 100vw, 586px\" \/><\/figure>\n<\/div>\n<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>\n<p>Because I thought many people will probably have the same problem, I&#8217;ll share this code here. So, let&#8217;s get started! <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f447.png\" alt=\"\ud83d\udc47\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4ac.png\" alt=\"\ud83d\udcac\" 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>\n<h2>The Code<\/h2>\n<div class=\"wp-block-image\">\n<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>\n<\/div>\n<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>\n<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\nimport time url = 'https:\/\/en.wikipedia.org\/wiki\/Graph_partition'\nword = 'finxter'\nx = 60 # seconds between two requests # Repeat forever\nwhile 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)\n<\/pre>\n<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>\n<h2>Code Explanation<\/h2>\n<div class=\"wp-block-image\">\n<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=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n<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>\n<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>\n<p class=\"has-base-background-color has-background\"> <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" 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 &#8211; Understanding <code>get()<\/code><\/a><\/p>\n<p>Then you check if the word appears in the response text using <code>r.text<\/code>.<\/p>\n<ul>\n<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>\n<li>If it does not, it prints the message <code>\"The word does not appear on the website\"<\/code>. <\/li>\n<\/ul>\n<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>\n<p>You can stop the code by hitting <code>CTRL+C<\/code>. <\/p>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" 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>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) I host multiple websites such as https:\/\/finxter.com, https:\/\/blog.finxter.com, and https:\/\/pythononeliners.com. 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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-131104","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/131104","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=131104"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/131104\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=131104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=131104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=131104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}