擅长:python、mysql、java
<p>将结果保存到内存中,然后只执行一个操作<code>json.dump</code>即可解决问题:</p>
<pre><code>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)
</code></pre>