要解决原始问题,例如:
with open("pathtomyfile", "w") as f:
for item in r.json or []:
try:
f.write(item['repository']['name'] + "\n")
except KeyError: # you might have to adjust what you are writing accordingly
pass # or sth ..
请注意,并非每个项目都是存储库,还有要点事件(等?)。
更好的是,将 json 保存到文件中。
#!/usr/bin/python
import json
import requests
r = requests.get('https://github.com/timeline.json')
with open("yourfilepath.json", "w") as f:
f.write(json.dumps(r.json))
然后,您可以打开它:
with open("yourfilepath.json", "r") as f:
obj = json.loads(f.read())