Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.26 KB | None | 0 0
  1. import requests
  2. from bs4 import BeautifulSoup
  3.  
  4. URL = 'https://ru.wikipedia.org/wiki/%D0%A3%D0%BF%D0%BE%D0%BB%D0%BD%D0%BE%D0%BC%D0%BE%D1%87%D0%B5%D0%BD%D0%BD%D1%8B%D0%B9_%D0%BF%D0%BE_%D0%B7%D0%B0%D1%89%D0%B8%D1%82%D0%B5_%D0%BF%D1%80%D0%B0%D0%B2_%D0%BF%D1%80%D0%B5%D0%B4%D0%BF%D1%80%D0%B8%D0%BD%D0%B8%D0%BC%D0%B0%D1%82%D0%B5%D0%BB%D0%B5%D0%B9_%D0%B2_%D0%A0%D0%BE%D1%81%D1%81%D0%B8%D0%B8'
  5. HEADERS = {
  6.     'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
  7.     'accept': '*/*'}
  8. HOST = 'https://ru.wikipedia.org'
  9.  
  10. def get_dig_date(str, mode=1):
  11.     dictt = {'января': 1, 'янв': 1, 'jan': 1, 'january': 1, '1': 1, '01': 1,
  12.             'февраля': 2, 'фев': 2, 'feb': 2, 'february': 2, '2': 2, '02': 2,
  13.             'марта': 3, 'march': 3, '3': 3, '03': 3,
  14.             'апреля': 4, 'april': 4, '4': 4, '04': 4,
  15.             'мая': 5, 'may': 5, '5': 5, '05': 5,
  16.             'июня': 6, 'june': 6, '6': 6, '06': 6,
  17.             'июля': 7, 'july': 7, '7': 7, '07': 7,
  18.             'августа': 8, 'august': 8, '8': 8, '08': 8,
  19.             'сентября': 9, 'september': 9, '9': 9, '09': 9,
  20.             'октября': 10, 'october': 10, '10': 10,
  21.             'ноября': 11, 'november': 11, '11': 11,
  22.             'декабря': 12, 'december': 12, '12': 12,
  23.     }
  24.     str = str.replace('-', ' ')
  25.     list = str.split()
  26.     ind_d = 0
  27.     ind_m = 1
  28.     ind_y = 2
  29.     if mode == 2:
  30.         ind_d = 2
  31.         ind_m = 1
  32.         ind_y = 0
  33.  
  34.     lst = {'day': int(list[ind_d]), 'month': dictt[list[ind_m]], 'year': int(list[ind_y])}
  35.     return lst
  36.  
  37.  
  38. def get_html(url, params=None):
  39.     html = requests.get(url, headers=HEADERS, params=params)
  40.     return html
  41.  
  42.  
  43. def get_content(html, mode):
  44.     soup = BeautifulSoup(html.text, 'html.parser')
  45.     table = soup.find('table', class_='infobox')
  46.     image_box = table.find('td', class_='infobox-image')
  47.     p_link = HOST + table.find('span', class_='no-wikidata').find_next('a').get('href')
  48.     p_soup = BeautifulSoup(get_html(p_link).text, 'html.parser')
  49.     p_box = p_soup.find('td', class_='plainlist')
  50.  
  51.     person = {
  52.         'name': table.find('span', class_='no-wikidata').get_text(),
  53.         'p_link': p_link,
  54.         'b_date': p_box.find_next('span', class_='wikidata-claim').find_next('a').get_text() + " " + p_box.find_next('span', class_='wikidata-claim').find_next('a').find_next('a').get_text(),
  55.         'ef_date': table.find('span', class_='no-wikidata').find_next('a').find_next('a').get_text() + " " + table.find('span', class_='no-wikidata').find_next('a').find_next('a').find_next('a').get_text(),
  56.         'image_link': 'https:' + image_box.find('img').get('src')
  57.     }
  58.     return person
  59.  
  60. def parse():
  61.     html = get_html(URL)
  62.     person = get_content(html)
  63.     name = {'first_name': person['name'].split()[0],
  64.             'middle_name': person['name'].split()[1],
  65.             'last_name': person['name'].split()[2]
  66.             }
  67.     content = {
  68.     'name': name,
  69.     'p_link': person['p_link'],
  70.     'b_date': get_dig_date(person['b_date']),
  71.     'ef_date': get_dig_date(person['ef_date'].replace('года', '')),
  72.     'image_link': person['image_link']
  73.     }
  74.     print(content)
  75.  
  76.  
  77. parse()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement