@s1zen

Как сравнить 2 словаря в парсинге?

Мне нужно сравнить 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)
  • Вопрос задан
  • 92 просмотра
Пригласить эксперта
Ответы на вопрос 1
leahch
@leahch
3D специалист. Dолго, Dорого, Dерьмово.
Используйте set и операцию дизьюнкции ^
Примеры в ответе здесь https://stackoverflow.com/questions/32815640/how-t...
Или здесь https://www.google.com/amp/s/www.geeksforgeeks.org...
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
30 апр. 2024, в 15:11
500 руб./в час
30 апр. 2024, в 15:11
5000 руб./за проект
30 апр. 2024, в 15:05
5000 руб./за проект