Guest User

Untitled

a guest
Aug 19th, 2022
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.81 KB | None | 0 0
  1. import json
  2. import os
  3.  
  4. import requests
  5. import openpyxl
  6. from tqdm import tqdm
  7.  
  8.  
  9. def get_data():
  10.     if not os.path.exists('images'):
  11.         os.mkdir('images')
  12.     book = openpyxl.Workbook()
  13.     for sheet in book.worksheets:
  14.         sheet.append(['Название', 'Описание', 'Идентификатор', 'Учредитель', 'Сокращенное название',
  15.                       'Тип собственности', 'КОПУК', 'Тип по профилю', 'ОГРН', 'ИНН', 'Юр. адрес',
  16.                       'Фактическое нахождение', 'Телефон', 'Email', 'Сайт', 'Руководитель', 'ОКПО',
  17.                       'Путь до фотографий'])
  18.     params = {
  19.         'limit': '20000',
  20.         'offset': '0',
  21.     }
  22.     r = requests.get('https://goskatalog.ru/muzfo-rest/rest/museums', params=params)
  23.     data = r.json()
  24.     museums = data.get('museums')
  25.     for museum in museums[:100]:
  26.         try:
  27.             inn = okpo = ogrn = address = mail = number = website = actual_address = headmaster = own_type = '-'
  28.             i = 1
  29.             id = museum.get('id')
  30.             url = f'https://goskatalog.ru/muzfo-rest/rest/museums/{id}'
  31.             r = requests.get(url)
  32.             data = r.json()
  33.             title = data.get('name')  # название
  34.             abbreviation = data.get('abbreviation')  # сокращенное название
  35.             annotation = data.get('annotation')  # описание
  36.             if annotation == None:
  37.                 annotation = '-'
  38.             else:
  39.                 annotation = annotation.replace('\n', '').replace('\r', '').replace('\t', '').strip()
  40.             founder = data.get('founder')  # основатель
  41.             kopuk = data.get('code')  # копук
  42.             unic_identifier = data.get('fullUin')  # уникальный идентификатор
  43.             # with open('data2.json', 'w', encoding='utf-8') as file:
  44.             #     json.dump(data, file, indent=4, ensure_ascii=False)
  45.  
  46.             identifiers = data.get('identifiers')
  47.             for identifier in identifiers:
  48.                 name = identifier.get('type')
  49.                 value = identifier.get('value')
  50.                 if name == 'INN':
  51.                     inn = value  # инн
  52.                 elif name == 'OKPO':
  53.                     okpo = value  # окпо
  54.                 elif name == 'OGRN':
  55.                     ogrn = value  # огрн
  56.  
  57.             contacts = data.get('contacts')
  58.             for contact in contacts:
  59.                 value = contact.get('contactValue')
  60.                 if '@' in value:
  61.                     mail = value
  62.                 elif '+7' in value:
  63.                     number = value
  64.                 elif 'http' in value:
  65.                     website = value
  66.                 elif 'ул' in value or 'д' in value:
  67.                     if address != '-':
  68.                         if value != address:
  69.                             actual_address = value
  70.                             continue
  71.                     address = value
  72.  
  73.             if actual_address == '-':
  74.                 actual_address = address
  75.  
  76.             images = data.get('images')
  77.  
  78.             file_title = title.replace('\\', '.') \
  79.                              .replace('"', '').replace('|', '').replace('*', 'x').replace(':', '') \
  80.                              .replace('?', '.').replace('<', '').replace('>', '').replace('/', '.')\
  81.                              .replace('\n', '')[:200]
  82.             for image in images:
  83.                 code = image.get('code')
  84.                 id = image.get('id')
  85.                 image_url = f'https://goskatalog.ru/muzfo-imaginator/rest/images/public/350/{code}/{id}.jpg'
  86.                 r = requests.get(image_url)
  87.                 with open(f'images/{file_title}_{i}.jpg', 'wb') as file:
  88.                     file.write(r.content)
  89.                 i += 1
  90.  
  91.             profile_type = data.get('types')[0].get('name')  # тип по профилю
  92.  
  93.             employees = data.get('employees')
  94.             for employee in employees:
  95.                 if employee.get('roleId') == 1:
  96.                     headmaster = employee.get('name')  # руководитель
  97.  
  98.             ownershipid = data.get('ownershipId')
  99.             if ownershipid == 3:
  100.                 own_type = 'Муниципальная'
  101.             elif ownershipid == 2:
  102.                 own_type = 'Субъекта РФ'
  103.             elif ownershipid == 1:
  104.                 own_type = 'Федеральная'
  105.  
  106.             for sheet in book.worksheets:
  107.                 sheet.append([annotation])
  108.         except Exception as e:
  109.             print(e)
  110.         finally:
  111.             book.save('data.xlsx')
  112.  
  113.  
  114. def main():
  115.     get_data()
  116.  
  117.  
  118. if __name__ == '__main__':
  119.     main()
  120.  
Advertisement
Add Comment
Please, Sign In to add comment