gpt4 book ai didi

python - 如何在 python 中使用分页从 API 创建有效的 json 文件?

转载 作者:太空宇宙 更新时间:2023-11-04 02:32:53 24 4
gpt4 key购买 nike

我正在尝试将 Star Wars API 中的所有人拉到一个有效的 json 文件中。

API 限制了结果集和所有“人”跨越 9 个单独的调用(即“https://swapi.co/api/people/?page=1”、“https://swapi.co/api/people/?page=2”等)。

当我遍历多个请求时,我最终得到无效的 json,因为文档与现在的开始和结束括号之间没有逗号分隔符。

请帮我解决我在通话中做错的地方。

import requests
import json

for x in range(1,9):
response = requests.get("https://swapi.co/api/people/?page="+str(x))
data = response.json()

next_page = data["next"]
results = data["results"]

for result in results:
with open('data.json', 'a') as outfile:
json.dump(result, outfile)
print('Done!')

json文件输出:

{"name": "Luke Skywalker", "height": "172", "mass": "77", "hair_color": "blond", "skin_color": "fair", "eye_color": "blue", "birth_year": "19BBY", "gender": "male", "homeworld": "https://swapi.co/api/planets/1/", "films": ["https://swapi.co/api/films/2/", "https://swapi.co/api/films/6/", "https://swapi.co/api/films/3/", "https://swapi.co/api/films/1/", "https://swapi.co/api/films/7/"], "species": ["https://swapi.co/api/species/1/"], "vehicles": ["https://swapi.co/api/vehicles/14/", "https://swapi.co/api/vehicles/30/"], "starships": ["https://swapi.co/api/starships/12/", "https://swapi.co/api/starships/22/"], "created": "2014-12-09T13:50:51.644000Z", "edited": "2014-12-20T21:17:56.891000Z", "url": "https://swapi.co/api/people/1/"}{"name": "C-3PO", "height": "167", "mass": "75", "hair_color": "n/a", "skin_color": "gold", "eye_color": "yellow", "birth_year": "112BBY", "gender": "n/a", "homeworld": "https://swapi.co/api/planets/1/", "films": ["https://swapi.co/api/films/2/", "https://swapi.co/api/films/5/", "https://swapi.co/api/films/4/", "https://swapi.co/api/films/6/", "https://swapi.co/api/films/3/", "https://swapi.co/api/films/1/"], "species": ["https://swapi.co/api/species/2/"], "vehicles": [], "starships": [], "created": "2014-12-10T15:10:51.357000Z", "edited": "2014-12-20T21:17:50.309000Z", "url": "https://swapi.co/api/people/2/"}

最佳答案

将您的结果保存到内存中,然后只执行一次 json.dump 即可解决您的问题:

import requests
import json

results = []
for x in range(1, 9):
response = requests.get("https://swapi.co/api/people/?page="+str(x))
data = response.json()

next_page = data["next"]
results.extend(data["results"])

with open('data.json', 'w') as outfile:
json.dump(results, outfile)

关于python - 如何在 python 中使用分页从 API 创建有效的 json 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48740198/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com