1.使用requests模块获取这个json文件http://java-api.super-yx.com/html/hello.json
2.将获取到的json转为dict
3.将dict保存为hello.json文件
4.用文件流写一个copy(src,dst)函数,复制hello.json到C:\hello.json
import requests
import json
def copy(src, dst):
read_file = open(src, "rb")
write_file = open(dst, "wb")
while True:
data = read_file.read(1024) # 每次读取 1024 字节
if not data: # 当读取到文件末尾时,data 为空
break
else:
write_file.write(data)
read_file.close()
write_file.close()
# 1. 使用 requests 模块获取 json 文件
response = requests.get('http://java-api.super-yx.com/html/hello.json')
# 2. 将获取到的 json 转为 dict
data_dict = json.loads(response.text)
# 3. 将 dict 保存为 hello.json 文件
with open('hello.json', 'w') as f:
json.dump(data_dict, f)
# 4. 复制 hello.json 到 D:\hello.json
copy('hello.json', 'D:\\hello.json')