Carson1

Untitled

Jun 8th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.60 KB | None | 0 0
  1. import requests
  2. from bs4 import BeautifulSoup
  3. import re
  4. import datetime
  5. import dateutil.parser as dparser
  6. class analyssaita:
  7.     def __init__(self):
  8.         self.page = requests.get('https://www.pkmpei.ru/')
  9.         self.soup = BeautifulSoup(self.page.text, 'html.parser')
  10.     def timetable(self):
  11.         timetable1 = []
  12.         timetable2 = []
  13.         timetable3 = []
  14.         timetable4 = []
  15.         self.timetable = dict()
  16.         s = ''
  17.         l = 0
  18.         timetable = str(self.soup.find(class_='title2'))
  19.         timetable = re.split('; ', timetable)
  20.         for i in timetable:
  21.             if 'left' in i or 'center' in i:
  22.                 timetable1.append(i)
  23.         for i in timetable1:
  24.             if '<b>' in i:
  25.                 timetable2.append(i)
  26.                 timetable2 = re.split(';', str(timetable2))
  27.                 for i in timetable2:
  28.                     if '"><b>' in i:
  29.                         start = '"><b>'
  30.                         end = '</b></td>\\n<td style="text-align: center'
  31.                         i = i[i.find(start)+len(start):i.rfind(end)]
  32.                         timetable3.append(i)
  33.                 for i in timetable2:
  34.                     if '\\xa0' in i:
  35.                         if 'Выходной' in i:
  36.                             i = 'Выходной'
  37.                         else:
  38.                             i = re.findall('\d+', i)
  39.                             for j in range(len(i) - 2):
  40.                                 if i[j] == '0':
  41.                                     del i[j]
  42.                                 elif i[j] == '010':
  43.                                     i[j] = '10'
  44.                                 elif i[-1] == '0':
  45.                                     del i[-1]
  46.                         i = ''.join(map(str, i))
  47.                         timetable4.append(i)
  48.                 for i in range(len(timetable4)):
  49.                     if timetable4[i] == '010001700':
  50.                         timetable4[i] = '10:00 - 17:00'
  51.                     elif timetable4[i] == '10001700':
  52.                         timetable4[i] = '10:00 - 17:00'
  53.                     elif timetable4[i] == '10001600':
  54.                         timetable4[i] = '10:00 - 16:00'
  55.         for i in range(len(timetable3)):
  56.             self.timetable[l] = timetable3[i] + ': ' + timetable4[i]
  57.             l = l +1
  58.         print(self.timetable)
  59.     def adress(self):
  60.         adress = str(self.soup(class_="text"))
  61.         start = 'Наш адрес'
  62.         end = '/><br/><h1'
  63.         adress = adress[adress.find(start)+len(start):adress.rfind(end)]
  64.         start1 = '<div style="text-align: left;">'
  65.         end1 = ', аудитория Б-209<br'
  66.         adress1 = adress[adress.find(start1)+len(start1):adress.rfind(end1)]
  67.         print(adress1)
  68.         start2 = '<div style="text-align: left;">'
  69.         end2 = '<br'
  70.         self.adress2 = adress[adress.find(start2)+len(start2):adress.rfind(end2)]
  71.         adress1 =adress1.replace(' ', '')
  72.         self.adress1 = 'https://www.google.com/search?q=' + adress1
  73.         print(self.adress1)
  74.     def contactniedannie(self):
  75.         contactniedannie = str(self.soup(class_="text"))
  76.         start = 'Контактные данные'
  77.         end = '</span><br/> </b></b>'
  78.         start1 = '</h1> <br/> <b>'
  79.         end1 = '<b><br/>'
  80.         contactniedannie = contactniedannie[contactniedannie.find(start)+len(start):contactniedannie.rfind(end)] + 'JFKLDSJFLSD'
  81.         self.telefon = contactniedannie[contactniedannie.find(start1)+len(start1):contactniedannie.rfind(end1)]
  82.         print(self.telefon)
  83.         start3 = '<span style="color: rgb(0, 0, 255);">'
  84.         end3 = 'JFKLDSJFLSD'
  85.         self.email = 'e-mail: ' + contactniedannie[contactniedannie.find(start3)+len(start3):contactniedannie.rfind(end3)]
  86.         print(self.email)
  87.     def novosti(self):
  88.         novosti = str(self.soup.find_all(id="right_news_item"))
  89.         novosti1 = []
  90.         data = []
  91.         data2 = []
  92.         d = re.split(',', novosti)
  93.         self.novosti = dict()
  94.         urls = []
  95.         news = []
  96.         for i in d:
  97.             if '<div id="right_news_item"' in i:
  98.                 novosti1.append(i)
  99.        
  100.         #print(novosti1)
  101.         for i in range(len(novosti1)):
  102.             start = 'href="'
  103.             end ='">\n<div>'
  104.             l = novosti1[i][novosti1[i].find(start)+len(start):novosti1[i].rfind(end)]
  105.             url = 'https://www.pkmpei.ru/' + l
  106.             urls.append(url)
  107.         for i in urls:
  108.             page = requests.get(i)
  109.             soup = BeautifulSoup(page.text, 'html.parser')
  110.             data1= str(soup.find(class_= 'title3'))
  111.             start1 = '<div class="title3">'
  112.             end1 = '</div>'
  113.             data1 = data1[data1.find(start1)+len(start1):data1.rfind(end1)]
  114.             data.append(data1)
  115.             for i in data:
  116.                 if i not in data2:
  117.                     data2.append(i)
  118.             text = str(soup.find_all(class_= 'text'))
  119.             start2 ='<h1>'
  120.             end2 = '</h1>'
  121.             text = text[text.find(start2)+len(start2):text.rfind(end2)]
  122.             news.append(text)
  123.             for i in range(len(news)):
  124.                 if '</h1>\n<br><h1>' in news[i]:
  125.                     news[i] = news[i][news[i].find('<h1>'):]
  126.                     news[i] = news[i][4:]
  127.             for i in range(len(data2)):
  128.                 self.novosti[data2[i]] = 'НОВОСТИ:' + '\n' + data2[i] + '\n' + news[i] + '\n' + 'Ссылка: ' + urls[i]
  129.             print(self.novosti)
  130. t = analyssaita()
  131. print(t.timetable())
  132. print(t.adress())
  133. print(t.contactniedannie())
  134. print(t.novosti())
Advertisement
Add Comment
Please, Sign In to add comment