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")