Advertisement
Guest User

Untitled

a guest
Apr 10th, 2020
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.13 KB | None | 0 0
  1. import requests
  2. from bs4 import BeautifulSoup
  3. import notify2
  4.  
  5. URL = "https://sverhestestvennoe.live/seasons/15s-06/"
  6. # имитация браузера, а не скрипта
  7. HEADERS = {'user-agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36',
  8. 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9'}
  9.  
  10. # params - это опциональный параметр, который иногда присутствует, к примеру, это может быть номер страницы на сайте
  11. def get_html(url):
  12.     r = requests.get(url, headers = HEADERS,);
  13.     return r
  14.  
  15. def get_content(html):
  16.     # в конструкторе объекта BF в качестве второго аргумента передан тип документа с которым мы работаем
  17.     soup = BeautifulSoup(html, 'html.parser')
  18.     episods = soup.find('ul', class_ = 'episodios').find_all('div', class_= 'numerando')# find_all - return list
  19.     numeric = len(episods)+1
  20.     try:
  21.         with open('old_series.txt', 'r') as f:
  22.             old_value = int(f.read())
  23.     except IOError:
  24.         with open('old_series.txt', 'w') as f:
  25.             f.write(str(numeric)); 
  26.     if(old_value != numeric):
  27.         print("New episod!")
  28.         notify2.init("backup")
  29.         n = notify2.Notification("BBR Checker Series", "New episod")
  30.         n.show()
  31.         with open('old.series.txt', 'w') as f:
  32.             f.write(str(numeric));
  33.     else:
  34.         print("No new episod!")
  35.     # part = [] # словарь для серии и названия
  36.     #for episod in episods:
  37.     #   part.append({
  38.     #       'data of reliase': episod.find('span', class_='date').get_text()
  39.     #       })
  40.  
  41. def parse():
  42.     html = get_html(URL);
  43.      # при выводе print(html.status_code) получим response[200], а все коды, что меньше, чем 400, это корректно совершенный запрос.
  44.     if html.status_code == 200:
  45.         get_content(html.text) # передаем готовый html в качестве текста
  46.        
  47.     else:
  48.         print('Error')
  49.  
  50. if __name__ == '__main__':
  51.     parse()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement