From d32f3fda9c7e011a30c075e426b7974ce2887fbc Mon Sep 17 00:00:00 2001 From: knyaz <31101995github> Date: Fri, 19 Feb 2021 18:16:47 +0300 Subject: [PATCH 1/2] lesson1 --- .gitignore | 3 ++- parser.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 parser.py diff --git a/.gitignore b/.gitignore index ecbfcaa..f1f3beb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .idea -.venv \ No newline at end of file +.venv +products \ No newline at end of file diff --git a/parser.py b/parser.py new file mode 100644 index 0000000..98f35ee --- /dev/null +++ b/parser.py @@ -0,0 +1,64 @@ +import requests +import json + +from pathlib import Path +from time import sleep + + +class Parser: + def __init__(self, url: str, path: Path): + self.url = url + self.path = path + + def _get_response(self, url, params): + while True: + response = requests.get(url=url, params=params) + if response.status_code == 200: + return response + sleep(1) + + def run(self): + response = requests.get(self.url) + for r in response.json(): + id = r["parent_group_code"] + name = r["parent_group_name"] + self._parse(id, name) + + + def _parse(self, id, name): + url = "https://5ka.ru/api/v2/special_offers/" + params = {"records_per_page": 12, + "page": 1, + "categories": id + } + response = self._get_response(url=url, params=params) + product_list = [] + + while url: + data = response.json() + url = data["next"] + try: + response = requests.get(url) + except: + continue + for product in data["results"]: + product_list.append(product) + + data = [{"name": name, "code": id, "products": product_list}] + save_path = self.path.joinpath(f"{id}.json") + self._save(data, save_path) + + @staticmethod + def _save(data, save_path): + jdata = json.dumps(data, ensure_ascii=False) + save_path.write_text(jdata, encoding="UTF-8") + + +if __name__ == "__main__": + url = "https://5ka.ru/api/v2/categories/" + path = Path(__file__).parent.joinpath("products") + if not path.exists(): + path.mkdir() + + parser = Parser(url, path) + parser.run() From 2049177785316666a412dde8719ef35606b35eeb Mon Sep 17 00:00:00 2001 From: knyaz <31101995github> Date: Fri, 26 Feb 2021 18:29:45 +0300 Subject: [PATCH 2/2] add test --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index f1f3beb..9c27713 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea .venv -products \ No newline at end of file +products +test \ No newline at end of file