Python-保存request请求为各种文件
文件下载相关工具
import json
import requests
class CustomFileTools(object):
def download_json_file(self, json_url, save_path):
"""
下载json文件并保存
"""
json_req = requests.get(json_url)
if json_req.status_code == 200:
json_conn = json.loads(json_req.content.decode('utf-8'))
with open(save_path, 'w') as wf:
json.dump(json_conn, wf)
else:
print(f"download json file error,url:{json_url}")
def download_file(self, file_url, save_path):
"""
下载普通文件并保存,如:txt,pdf,excel
"""
file_req = requests.get(file_url)
if file_req.status_code == 200:
file_conn = file_req.content
with open(save_path, 'wb') as wf:
wf.write(file_conn)
else:
print(f"download json file error,url:{file_url}")
if __name__ == '__main__':
test_obj = CustomFileTools()