如何在我的Python scraper中为Pokemon数据scraper正确格式化JSON输出?

nqwrtyyt  于 2023-05-30  发布在  Python
关注(0)|答案(1)|浏览(125)

我目前正在学习如何第一次使用json文件格式,并且一直在使用python开发一个口袋妖怪数据刮刀,它将所有数据刮取并输出到csv和json文件中。我目前正在努力正确地格式化json,以便很好地输出数据。目前,我将输出设置为:

data = {
            "name": name, #str
            "dex_num": dex_num, #int
            "sprite": sprite, #str
            "url_extension": url_extension, #str
            "types": types, #List[str]
            "total_stats": total_stats, #int
            "hp": hp, #int
            "attack": attack, #int
            "defense": defense, #int
            "sp_attack": sp_attack, #int
            "sp_defense": sp_defense, #int
            "speed": speed, #int
            "species": species, #str
            "height": height, #str
            "weight": weight, #str
            "abilities": abilities, #List[str]
            "hability": hability, #str
            "pdex_entry": pdex_entry, #List[str]
            "ev_yield": ev_yield, #str
            "catch_rate": catch_rate, #str
            "friendship": friendship, #str
            "base_xp": base_xp, #int
            "growth_rate": growth_rate, #str
            "gender": gender, #List[str]
            "egg_groups": egg_groups, #List[str]
            "egg_cycles": egg_cycles,#str
            "evo_line": evo_line, #List[str]
            "evo_method": evo_method, #List[str]
            "moves_list": moves_list #List[str]
        }

this的一个输出字段目前看起来如下所示:

{
        "name": "Baxcalibur",
        "dex_num": "998",
        "sprite": "https://img.pokemondb.net/sprites/scarlet-violet/icon/baxcalibur.png",
        "url_extension": "/pokedex/baxcalibur",
        "types": [
            "Dragon",
            "Ice"
        ],
        "total_stats": "600",
        "hp": "115",
        "attack": "145",
        "defense": "92",
        "sp_attack": "75",
        "sp_defense": "86",
        "speed": "87",
        "species": "Ice Dragon Pokémon",
        "height": "2.1 m (6′11″)",
        "weight": "210.0 kg (463.0 lbs)",
        "abilities": [
            "Thermal Exchange"
        ],
        "hability": "Ice Body (hidden ability)",
        "pdex_entry": [
            "This Pokémon blasts cryogenic air out from its mouth. This air can instantly freeze even liquid-hot lava.",
            "It launches itself into battle by flipping upside down and spewing frigid air from its mouth. It finishes opponents off with its dorsal blade."
        ],
        "ev_yield": "\n3 Attack ",
        "catch_rate": "\n10 (1.3% with PokéBall, full HP)\n",
        "friendship": "\n50 (normal)\n",
        "base_xp": "300",
        "growth_rate": "Slow",
        "gender": [
            "50% male, 50% female"
        ],
        "egg_groups": [
            "Dragon",
            "Mineral"
        ],
        "egg_cycles": "40 (10,024–10,280 steps)\n",
        "evo_line": [
            "Frigibax",
            "Arctibax",
            "Baxcalibur"
        ],
        "evo_method": [
            "(Level 35)",
            "(Level 54)"
        ],
        "moves_list": [
            [
                "Breaking Swipe",
                "Dragon",
                "Physical",
                "60",
                "100"
            ],
            [
                "Dragon Tail",
                "Dragon",
                "Physical",
                "60",
                "90"
            ]
           #Moves list is longer but you get the idea
        ]
    }

我希望它看起来像这样:

{
        "name": "Baxcalibur",
        "dex_num": "998",
        "sprite": "https://img.pokemondb.net/sprites/scarlet-violet/icon/baxcalibur.png",
        "url_extension": "/pokedex/baxcalibur",
        "types": ["Dragon", "Ice"],
        "total_stats": "600",
        "hp": "115",
        "attack": "145",
        "defense": "92",
        "sp_attack": "75",
        "sp_defense": "86",
        "speed": "87",
        "species": "Ice Dragon Pokémon",
        "height": "2.1 m (6′11″)",
        "weight": "210.0 kg (463.0 lbs)",
        "abilities": ["Thermal Exchange"],
        "hability": "Ice Body (hidden ability)",
        "pdex_entry": ["This Pokémon blasts cryogenic air out from its mouth. This air can instantly freeze even liquid-hot lava.", "It launches itself into battle by flipping upside down and spewing frigid air from its mouth. It finishes opponents off with its dorsal blade."],
        "ev_yield": "\n3 Attack ",
        "catch_rate": "\n10 (1.3% with PokéBall, full HP)\n",
        "friendship": "\n50 (normal)\n",
        "base_xp": "300",
        "growth_rate": "Slow",
        "gender": ["50% male, 50% female"],
        "egg_groups": ["Dragon", "Mineral"],
        "egg_cycles": "40 (10,024–10,280 steps)\n",
        "evo_line": ["Frigibax", "Arctibax", "Baxcalibur"],
        "evo_method": ["(Level 35)", "(Level 54)"],
        "moves_list": [["move_name": "Breaking Swipe", "move_type": "Dragon", "move_cat": "Physical", "move_power": "60", "move_accuracy": "100"], ["move_name": "Dragon Tail", "move_type": "Dragon", "move_cat": "Physical", "move_power": "60", "move_accuracy": "90"] #Longer but you get the idea]
    }

谢谢你的帮助。
我试着按照其他教程,并在这里寻找其他问题,但无法找出如何格式的权利

svmlkihl

svmlkihl1#

您可以修改生成"moves_list"数据的代码。不应该附加一个移动详细信息列表,而应该附加一个dictionary,其中包含与所需JSON结构匹配的键。
JSON看起来像这样:

{
    "name": "Baxcalibur",
    "dex_num": "998",
    "sprite": "https://img.pokemondb.net/sprites/scarlet-violet/icon/baxcalibur.png",
    "url_extension": "/pokedex/baxcalibur",
    "types": ["Dragon", "Ice"],
    "total_stats": "600",
    "hp": "115",
    "attack": "145",
    "defense": "92",
    "sp_attack": "75",
    "sp_defense": "86",
    "speed": "87",
    "species": "Ice Dragon Pokémon",
    "height": "2.1 m (6′11″)",
    "weight": "210.0 kg (463.0 lbs)",
    "abilities": ["Thermal Exchange"],
    "hability": "Ice Body (hidden ability)",
    "pdex_entry": ["This Pokémon blasts cryogenic air out from its mouth. This air can instantly freeze even liquid-hot lava.", "It launches itself into battle by flipping upside down and spewing frigid air from its mouth. It finishes opponents off with its dorsal blade."],
    "ev_yield": "\n3 Attack ",
    "catch_rate": "\n10 (1.3% with PokéBall, full HP)\n",
    "friendship": "\n50 (normal)\n",
    "base_xp": "300",
    "growth_rate": "Slow",
    "gender": ["50% male, 50% female"],
    "egg_groups": ["Dragon", "Mineral"],
    "egg_cycles": "40 (10,024–10,280 steps)\n",
    "evo_line": ["Frigibax", "Arctibax", "Baxcalibur"],
    "evo_method": ["(Level 35)", "(Level 54)"],
    "moves_list": [
        {
            "move_name": "Breaking Swipe",
            "move_type": "Dragon",
            "move_cat": "Physical",
            "move_power": "60",
            "move_accuracy": "100"
        },
        {
            "move_name": "Dragon Tail",
            "move_type": "Dragon",
            "move_cat": "Physical",
            "move_power": "60",
            "move_accuracy": "90"
        }

] }
然后,我们必须修改抓取代码,以便它为每次移动创建一个字典,然后将其附加到moves_list

moves_list = []

for move in moves:
    move_dict = {
        "move_name": move[0],
        "move_type": move[1],
        "move_cat": move[2],
        "move_power": move[3],
        "move_accuracy": move[4]
    }
    moves_list.append(move_dict)

相关问题