python作业三

1.使用requests模块获取这个json文件http://java-api.super-yx.com/html/hello.json
2.将获取到的json转为dict
3.将dict保存为hello.json文件
4.用io流写一个copy(src,dst)函数,复制hello.json到C:\hello.json

import json
import shutil

import requests
#使用requests模块获取这个json文件http://java-api.super-yx.com/html/hello.json
#将获取到的json转为dict
data_dict={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
                    "AppleWebKit/427.36 (KHTML, like Gecko)"
                        " Chrome/126.0.0.0 Safari/427.36"}
response = requests.get("http://java-api.super-yx.com/html/hello.json",
                        headers=data_dict)
print(response)
print(response.text)
#将dict保存为hello.json文件
with open('hello.josn','w',encoding='utf-8') as f:
    json.dump(data_dict,f,ensure_ascii=False,indent=4)
#用io流写一个copy(src,dst)函数,复制hello.json到C:\hello.json
def copy(src,dst):
    with open(src,'rb') as src_file:
        with open(dst,'wb') as dst_file:
            shutil.copyfileobj(src_file,dst_file)

copy('hello.josn',r'D:\hello.josn')


代码运行结果

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-15 18:46:02       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-15 18:46:02       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-15 18:46:02       57 阅读
  4. Python语言-面向对象

    2024-07-15 18:46:02       68 阅读

热门阅读

  1. 动态内存管理(C)

    2024-07-15 18:46:02       23 阅读
  2. 算法的时间复杂度和空间复杂度-概念

    2024-07-15 18:46:02       19 阅读
  3. Matlab

    Matlab

    2024-07-15 18:46:02      20 阅读
  4. C/C++指针&智能指针二

    2024-07-15 18:46:02       15 阅读