Skip to content

Instantly share code, notes, and snippets.

@ap-Codkelden
Created December 24, 2019 11:58
Show Gist options
  • Save ap-Codkelden/c3c0e14a9fbef38e6b1ebad8280df59d to your computer and use it in GitHub Desktop.
Save ap-Codkelden/c3c0e14a9fbef38e6b1ebad8280df59d to your computer and use it in GitHub Desktop.
eliky API
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests # http requests
import time
import json
E_SERV = "https://eliky.in.ua" # server address
path = "/api/medicament" # server path
params = {
"api_key": "***", # ключ
"page": None # лічильник сторінок
}
s = requests.Session()
headers = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:67.0) Gecko/20100101 Firefox/67.0",
"Connection": "keep-alive",
}
eliky_box = [] # порожній список збереження результатів
page = 1 # лічильник сторінок
end = False # прапорець кінця збору
while not end:
params['page'] = page
url = E_SERV + path # конструюємо URL
res = s.get(url, headers=headers, params=params)
res_json = res.json()
if 'error' in res_json:
print("Помилка: " + res_json['error']['message'])
break
eliky_box.extend(res_json['result']) # додамо в список
total_pages = res_json["page"]["total"]
page += 1
if page > total_pages:
end = True
print("\nЗбір завершено, {} об'єктів отримано.\n".format(len(eliky_box)))
if page % 5 == 0 and page % 20 != 0:
print('.', end='', flush=True)
elif page % 20 == 0:
print(page, end='', flush=True)
time.sleep(1.5) # 1,5 секунди
with open('eliky.json', 'w') as f:
json.dump(eliky_box, f, ensure_ascii=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment