从网站下载包含数组的JSON响应。

huangapple go评论84阅读模式
英文:

Download JSON response from website with array

问题

我正试图从一个具有不同结尾的网站获取JSON响应,例如:

http//example.com/111
http//example.com/112
http//example.com/113
...
http//example.com/200

每个请求都会给我一个具有相同列但不同数据的JSON:

{
	"uid": 111,
	"name": "Pinky",
	"title": "Mouse",
	"type": "No brain"
}	

{
	"uid": 112,
	"name": "Brain",
	"title": "Big Mouse",
	"type": "Brain"
}

{
	"uid": 113,
	"name": "Garfield",
	"title": "Cat",
	"type": "lazy"
}	

我在一个Python脚本中遇到了问题,该脚本获取所有JSON数据并将其合并为一个来自定义数组的数据,例如:[111 - 200]

[
	{
		"uid": 111,
		"name": "Pinky",
		"title": "Mouse",
		"type": "No brain"
	},
	{
		"uid": 112,
		"name": "Brain",
		"title": "Big Mouse",
		"type": "Brain"
	},
	{
		"uid": 113,
		"name": "Garfield",
		"title": "Cat",
		"type": "lazy"
	}
]
英文:

I'm trying to get a JSON response from a website that has a different ending, for example:

http//example.com/111
http//example.com/112
http//example.com/113
...
http//example.com/200

Every request gives me JSON with the same columns but different data:

{
	"uid": 111,
	"name": "Pinky",
	"title": "Mouse",
	"type": "No brain"
}	

{
	"uid": 112,
	"name": "Brain",
	"title": "Big Mouse",
	"type": "Brain"
}

{
	"uid": 113,
	"name": "Garfield",
	"title": "Cat",
	"type": "lazy"
}	

I'm having trouble with a Python script that get all the JSON data and unites it into one from the defined array, for example: [111 - 200]

[
	{
		"uid": 111,
		"name": "Pinky",
		"title": "Mouse",
		"type": "No brain"
	},
	{
		"uid": 112,
		"name": "Brain",
		"title": "Big Mouse",
		"type": "Brain"
	},
	{
		"uid": 113,
		"name": "Garfield",
		"title": "Cat",
		"type": "lazy"
	}
]

Please assist.

答案1

得分: 0

你可以将结果存储在一个列表中,然后一次性保存为JSON文件。

这里以PokeAPI为例:

from bs4 import BeautifulSoup
import json
import requests

pokemons = []
for poke_id in range(1, 7):
    url = f'https://pokeapi.co/api/v2/pokemon/{poke_id}'
    
    data = requests.get(url).json()
    data = {
        'id': data.pop('id'),
        'types': data.pop('types'),
        'base_experience': data.pop('base_experience')
    } # 仅筛选了一些属性
    
    print(data)
    pokemons.append(data)

with open('pokemon.json', 'w') as f:
    json.dump(pokemons, f)

每次循环迭代时,我都将一个网页的结果添加到列表中,最后将其写入JSON文件。

以下是输出结果:

[
    {
        "id": 1,
        "types": [
            {
                "slot": 1,
                "type": {
                    "name": "grass",
                    "url": "https://pokeapi.co/api/v2/type/12/"
                }
            },
            {
                "slot": 2,
                "type": {
                    "name": "poison",
                    "url": "https://pokeapi.co/api/v2/type/4/"
                }
            }
        ],
        "base_experience": 64
    },
    {
        "id": 2,
        "types": [
            {
                "slot": 1,
                "type": {
                    "name": "grass",
                    "url": "https://pokeapi.co/api/v2/type/12/"
                }
            },
            {
                "slot": 2,
                "type": {
                    "name": "poison",
                    "url": "https://pokeapi.co/api/v2/type/4/"
                }
            }
        ],
        "base_experience": 142
    },
    {
        "id": 3,
        "types": [
            {
                "slot": 1,
                "type": {
                    "name": "grass",
                    "url": "https://pokeapi.co/api/v2/type/12/"
                }
            },
            {
                "slot": 2,
                "type": {
                    "name": "poison",
                    "url": "https://pokeapi.co/api/v2/type/4/"
                }
            }
        ],
        "base_experience": 263
    },
    {
        "id": 4,
        "types": [
            {
                "slot": 1,
                "type": {
                    "name": "fire",
                    "url": "https://pokeapi.co/api/v2/type/10/"
                }
            }
        ],
        "base_experience": 62
    },
    {
        "id": 5,
        "types": [
            {
                "slot": 1,
                "type": {
                    "name": "fire",
                    "url": "https://pokeapi.co/api/v2/type/10/"
                }
            }
        ],
        "base_experience": 142
    },
    {
        "id": 6,
        "types": [
            {
                "slot": 1,
                "type": {
                    "name": "fire",
                    "url": "https://pokeapi.co/api/v2/type/10/"
                }
            },
            {
                "slot": 2,
                "type": {
                    "name": "flying",
                    "url": "https://pokeapi.co/api/v2/type/3/"
                }
            }
        ],
        "base_experience": 267
    }
]
英文:

You can store the results in a list and save them to JSON at once.

I am taking PokeAPI as an example here.

from bs4 import BeautifulSoup
import json
import requests
pokemons = []
for poke_id in range(1, 7):
url = f'https://pokeapi.co/api/v2/pokemon/{poke_id}'
data = requests.get(url).json()
data = {
'id':data.pop('id'),
'types': data.pop('types'),
'base_experience': data.pop('base_experience')
} # to filter a few attributes only
print(data)
pokemons.append(data)
with open('pokemon.json', 'w') as f:
json.dump(pokemons, f)

With each iteration of loop, I am appending a webpage's result to the list, and at last writing to the JSON file.

This is the output:

[
{
"id": 1,
"types": [
{
"slot": 1,
"type": {
"name": "grass",
"url": "https://pokeapi.co/api/v2/type/12/"
}
},
{
"slot": 2,
"type": {
"name": "poison",
"url": "https://pokeapi.co/api/v2/type/4/"
}
}
],
"base_experience": 64
},
{
"id": 2,
"types": [
{
"slot": 1,
"type": {
"name": "grass",
"url": "https://pokeapi.co/api/v2/type/12/"
}
},
{
"slot": 2,
"type": {
"name": "poison",
"url": "https://pokeapi.co/api/v2/type/4/"
}
}
],
"base_experience": 142
},
{
"id": 3,
"types": [
{
"slot": 1,
"type": {
"name": "grass",
"url": "https://pokeapi.co/api/v2/type/12/"
}
},
{
"slot": 2,
"type": {
"name": "poison",
"url": "https://pokeapi.co/api/v2/type/4/"
}
}
],
"base_experience": 263
},
{
"id": 4,
"types": [
{
"slot": 1,
"type": {
"name": "fire",
"url": "https://pokeapi.co/api/v2/type/10/"
}
}
],
"base_experience": 62
},
{
"id": 5,
"types": [
{
"slot": 1,
"type": {
"name": "fire",
"url": "https://pokeapi.co/api/v2/type/10/"
}
}
],
"base_experience": 142
},
{
"id": 6,
"types": [
{
"slot": 1,
"type": {
"name": "fire",
"url": "https://pokeapi.co/api/v2/type/10/"
}
},
{
"slot": 2,
"type": {
"name": "flying",
"url": "https://pokeapi.co/api/v2/type/3/"
}
}
],
"base_experience": 267
}
]

huangapple
  • 本文由 发表于 2023年7月14日 03:40:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76682732.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定