Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将json文件保存到计算机python_Python_Python Requests - Fatal编程技术网

将json文件保存到计算机python

将json文件保存到计算机python,python,python-requests,Python,Python Requests,使用Python中的requests,我正在执行一个GET,请求一个JSON文件,我可以稍后使用命令solditems.JSON()访问、修改、调整等。但是,我想将这个JSON文件保存到我的计算机上。通过查看请求文档,我没有发现任何东西,有没有人能找到一种简单的方法让我这样做?你可以像没有请求时那样做。您的代码可能看起来像 import json import requests solditems = requests.get('https://github.com/timeline.json

使用Python中的
requests
,我正在执行一个GET,请求一个JSON文件,我可以稍后使用命令
solditems.JSON()
访问、修改、调整等。但是,我想将这个JSON文件保存到我的计算机上。通过查看请求文档,我没有发现任何东西,有没有人能找到一种简单的方法让我这样做?

你可以像没有
请求时那样做。您的代码可能看起来像

import json
import requests

solditems = requests.get('https://github.com/timeline.json') # (your url)
data = solditems.json()
with open('data.json', 'w') as f:
    json.dump(data, f)

根据@Lukasa的评论,这加速了@Jared的解决方案:

导入请求
solditems=requests.get('https://github.com/timeline.json“)#(您的url)
数据=solditems.content
将open('data.json','wb')作为f:
f、 写入(数据)

使用标准库的新
pathlib
库,这也非常简单:

import requests
import pathlib

solditems = requests.get('https://github.com/timeline.json') # (your url)
pathlib.Path('data.json').write_bytes(solditems.content)

没有充分的理由对JSON进行反序列化然后重新序列化。只需将
solditems.content
直接写入文件。我的印象是,OP将在写入文件之前对其进行调整。“访问和修改、调整等”。这只是一个例子:)