Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import logging
- import os
- import openpyxl
- import requests
- from tqdm import tqdm
- def get_data():
- logging.basicConfig(
- level=logging.INFO
- )
- if not os.path.exists('images'):
- os.mkdir('images')
- book = openpyxl.Workbook()
- for sheet in book.worksheets:
- sheet.append(['Название', 'Описание', 'Идентификатор', 'Учредитель', 'Сокращенное название',
- 'Тип собственности', 'КОПУК', 'Тип по профилю', 'ОГРН', 'ИНН', 'Юр. адрес',
- 'Фактическое нахождение', 'Телефон', 'Email', 'Сайт', 'Руководитель', 'ОКПО',
- 'Путь до фотографий'])
- headers = {
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:103.0) Gecko/20100101 Firefox/103.0',
- 'Accept': 'application/json, text/plain, */*',
- 'Accept-Language': 'ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3',
- 'Connection': 'keep-alive',
- 'Referer': 'https://goskatalog.ru/portal/',
- 'Sec-Fetch-Dest': 'empty',
- 'Sec-Fetch-Mode': 'cors',
- 'Sec-Fetch-Site': 'same-origin',
- }
- params = {
- 'cacheEnabled': 'true',
- 'limit': '20000',
- 'offset': '0',
- 'publicationLimit': 'false',
- }
- r = requests.get('https://goskatalog.ru/muzfo-rest/rest/museums', params=params, headers=headers)
- data = r.json()
- museums = data.get('museums')
- for museum in tqdm(museums[:100]):
- try:
- inn = okpo = ogrn = address = mail = number = website = actual_address = headmaster = own_type = '-'
- i = 1
- id = museum.get('id')
- url = f'https://goskatalog.ru/muzfo-rest/rest/museums/{id}'
- r = requests.get(url, headers=headers)
- data = r.json()
- title = data.get('name') # название
- abbreviation = data.get('abbreviation') # сокращенное название
- annotation = data.get('annotation') # описание
- if annotation == None:
- annotation = '-'
- else:
- annotation = annotation.replace('\n', '').replace('\r', '').replace('\t', '').strip()
- founder = data.get('founder') # основатель
- kopuk = data.get('code') # копук
- unic_identifier = data.get('fullUin') # уникальный идентификатор
- # with open('data2.json', 'w', encoding='utf-8') as file:
- # json.dump(data, file, indent=4, ensure_ascii=False)
- identifiers = data.get('identifiers')
- for identifier in identifiers:
- name = identifier.get('type')
- value = identifier.get('value')
- if name == 'INN':
- inn = value # инн
- elif name == 'OKPO':
- okpo = value # окпо
- elif name == 'OGRN':
- ogrn = value # огрн
- contacts = data.get('contacts')
- for contact in contacts:
- value = contact.get('contactValue')
- if '@' in value:
- mail = value
- elif '+7' in value:
- number = value
- elif 'http' in value:
- website = value
- elif 'ул' in value or 'д' in value:
- if address != '-':
- if value != address:
- actual_address = value
- continue
- address = value
- if actual_address == '-':
- actual_address = address
- images = data.get('images')
- file_title = title.replace('\\', '.') \
- .replace('"', '').replace('|', '').replace('*', 'x').replace(':', '') \
- .replace('?', '.').replace('<', '').replace('>', '').replace('/', '.') \
- .replace('\n', '')[:200]
- for image in images:
- code = image.get('code')
- id = image.get('id')
- image_url = f'https://goskatalog.ru/muzfo-imaginator/rest/images/public/350/{code}/{id}.jpg'
- r = requests.get(image_url)
- with open(f'images/{file_title}_{i}.jpg', 'wb') as file:
- file.write(r.content)
- i += 1
- profile_type = data.get('types')[0].get('name') # тип по профилю
- employees = data.get('employees')
- for employee in employees:
- if employee.get('roleId') == 1:
- headmaster = employee.get('name') # руководитель
- ownershipid = data.get('ownershipId')
- if ownershipid == 3:
- own_type = 'Муниципальная'
- elif ownershipid == 2:
- own_type = 'Субъекта РФ'
- elif ownershipid == 1:
- own_type = 'Федеральная'
- if annotation != '-':
- logging.info(annotation + '\n')
- for sheet in book.worksheets:
- sheet.append([title, annotation, unic_identifier, founder, abbreviation,
- own_type, kopuk, profile_type, ogrn, inn, address,
- actual_address, number, mail, website, headmaster, okpo, f'images/{file_title}'])
- except Exception as e:
- print(e)
- finally:
- book.save('data.xlsx')
- def main():
- get_data()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment