Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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
Python JSON保存到文件获取方法_Python_Json_Selenium_Post_Get - Fatal编程技术网

Python JSON保存到文件获取方法

Python JSON保存到文件获取方法,python,json,selenium,post,get,Python,Json,Selenium,Post,Get,我的代码: def saveJSONget(site, code): response = requests.get(site) json_str = json.dumps(response.json()) if not response.status_code == code: file = open("PATH", 'w') file.write(str(response.text) + "\n"

我的代码:

def saveJSONget(site, code):
        response = requests.get(site)
        json_str = json.dumps(response.json())
        if not response.status_code == code:
            file = open("PATH", 'w')
            file.write(str(response.text) + "\n")
我需要保存到json文件的所有信息:头响应和请求,请求负载。我不知道该使用什么函数,即写入文件中的这个元素。我还有第二个问题

如何改变我的方法? 错误:


谢谢你的帮助

谢谢。如何编写->请求头、响应头和in-POST方法-请求负载?
r=requests.get(url,Headers={'key':'value'})
请参阅
from pathlib import Path
def saveJSONget(site, code):
    r = requests.get(site)
    json_str = r.text
    if r.status_code != code:
        Path("PATH").write_text(r.text+"\n")
from pathlib import Path
def saveJSONget(site, code):
    r = requests.get(site)
    json_str = r.text
    if r.status_code != code:
        Path("PATH").write_text(r.text+"\n")