Мне нужно сравнить 2 словаря, но походу я особо не понимаю как сделать это правильно.
Мне нужно добавлять данные если их нет в словаре.
Помогите реализовать!
async def kakao_get_data():
# Проверка создан ли файл
if os.path.exists("result.json") == False:
with open("result.json", "w", encoding="utf-8") as file:
file.write("")
# Чтение файла
with open("result.json", "r", encoding="utf-8") as file:
try:
src = json.load(file)
except json.decoder.JSONDecodeError:
src = {}
page_count = int(requests.get("https://api2-page.kakao.com/api/v2/store/today_up/list?category=10&subcategory=0&page=0", headers=headers).json()["total_count"] / 24) + 1
kakao_books_dict = {}
for page in range(page_count):
async with aiohttp.ClientSession(headers=headers) as session:
async with session.get(f"https://api2-page.kakao.com/api/v2/store/today_up/list?category=10&subcategory=0&page={page}") as response:
page_json = await response.json()
for i in range(len(page_json["list"])):
if page_json["list"][i]["badge"] == "BT03":
book_name = page_json["list"][i]["title"]
book_link = "https://page.kakao.com/home?seriesId=" + str(page_json["list"][i]["series_id"])
kakao_books_dict = {
"book_name": book_name,
"book_link": book_link
}
with open("result.json", "a", encoding="utf-8") as file:
json.dump(kakao_books_dict, file, ensure_ascii=False, indent=4)
# print(kakao_books_dict)
# try:
# if src_flag == True:
# with open("result.json", "a", encoding="utf-8") as file:
# json.dump(kakao_books_dict, file, ensure_ascii=False, indent=4)
# else:
# for item in src.items():
# if item in kakao_books_dict:
# continue
# else:
# with open("result.json", "a", encoding="utf-8") as file:
# json.dump(kakao_books_dict, file, ensure_ascii=False, indent=4)
# except:
# with open("result.json", "a", encoding="utf-8") as file:
# json.dump(kakao_books_dict, file, ensure_ascii=False, indent=4)
# with open("result.json", "a", encoding="utf-8") as file:
# try:
# new_result = set(src).difference(set(kakao_books_dict))
# json.dump(new_result, file, ensure_ascii=False, indent=4)
# except TypeError:
# json.dump(kakao_books_dict, file, ensure_ascii=False, indent=4)