import requests from bs4 import BeautifulSoup import telebot from time import sleep ###### WORK WITH FILES ##### import pickle def write(a_list): with open('list', 'wb') as fp: pickle.dump(a_list, fp) def read(): with open('list', 'rb') as fp: a_list = pickle.load(fp) return a_list ############################ def check(bot, own_id): response = requests.get("https://ss14top.404.mn/") soup = BeautifulSoup(response.content, 'html.parser') topics = [] for link in soup.find_all("a"): link = str(link.get("href")) if "topic" in link: topics.append(link) # Get only link to review below post links = [] nums = [*range(1,15)] for el in topics: check = el[:el.rfind("/")] if check not in links and check != "/topic/8/важное": links.append(check) ############## CHECK IF MESSAGE NEW ############ for i in links: response = requests.get(f"https://ss14top.404.mn/{i}") soup = BeautifulSoup(response.content, 'html.parser') # Add reviews and remove post (0) reviews = [] for review in soup.find_all("div", class_="content"): reviews.append(review.get_text()) try: reviews.pop(0) except: pass # Check if new rev try: old_reviews = read() except: old_reviews = [] for rev in reviews: if rev not in old_reviews: new = f"{telebot.formatting.hcode(f'https://ss14top.404.mn/{i}')} \n\n{rev}" try: bot.send_message(own_id, new, parse_mode="HTML") except: bot.send_message(own_id, new) # Write to reviews reviews = old_reviews + reviews write(reviews) sleep(1)