Jeste li ikad bjesomučno refreshali neku stranicu čekajući neku vijest ili promjenu? (Recimo, popis pozvanih na Državno natjecanje koji se danas očekuje?)
Možda ste znali, a možda i niste, da se to refreshanje može automatizirati.
Postoje online alati za to (recimo, https://www.followthatpage.com/), ali zašto ne bismo zasukali rukave i sami napisali odgovarajuću skripticu?
Ovo je moja:
""" Usage: refresh.py [full_url] [refresh_interval_in_seconds] [optional: string_to_search] If the search string is given, then refreshes until the string is found in page text. Otherwise, refreshes until the page is changed. """ import urllib.request import sys from time import sleep from bs4 import BeautifulSoup def get_text(url): text = str(urllib.request.urlopen(url).read()) soup = BeautifulSoup(text, 'html.parser') [s.extract() for s in soup(['style', 'script', '[document]', 'head', 'title'])] text = soup.getText() # ignore numbers for z in '0123456789': text = text.replace(z, '') return text try: interval = int(sys.argv[2]) url = sys.argv[1] except: print(f'usage: {sys.argv[0]} [full_url] [refresh_interval_in_seconds] [optional: string to search]') exit(0) if len(sys.argv) > 3: word = sys.argv[3] else: word = None print(f'reading {url} ...') previous_text = get_text(url) while True: sleep(interval) print(f'refreshing {url} ...', end=' ') text = get_text(url) if word: if word in text: print(f'String "{word}" found in page text!') exit(0) else: print(f'string "{word}" not found') else: if text != previous_text: print(f'Page has changed!') exit(0) else: print('no change') previous_text = text
Ovaj dio s promjenom ne radi na stranicama s oglasima (npr. portali) jer su svaki put drugačije reklame pa skripta misli da se stranica promijenila. Možda ima i još bugova ili prostora za poboljšanje, slobodno komentirajte.
Napomena: Ne postavljajte refresh interval na manje od nekoliko sekundi; prečesto slanje requestova može biti nepristojno ili čak zabranjeno. Related: https://softwareengineering.stackexchange.com/a/304767
Povratni ping: Kategorizacija blogaritamskih objava | Blogaritam