1.使用Python中的requests库向API发送请求并获取响应。
import requests
url = 'https://example.com/api/zipfile' response = requests.get(url)
2.将响应内容保存为二进制文件,可以使用Python内置的open()函数。
with open('saved_file.zip', 'wb') as f: f.write(response.content)
3.选择一个可访问的文件夹。将前面保存的ZIP文件移至此文件夹中,可以使用Python内置的shutil库。
import shutil, os
src_path = 'saved_file.zip' dst_folder = '/path/to/destination/folder'
shutil.move(src_path, os.path.join(dst_folder, os.path.basename(src_path)))
4.完整的代码示例:
import requests import shutil, os
url = 'https://example.com/api/zipfile' response = requests.get(url)
with open('saved_file.zip', 'wb') as f: f.write(response.content)
src_path = 'saved_file.zip' dst_folder = '/path/to/destination/folder'
shutil.move(src_path, os.path.join(dst_folder, os.path.basename(src_path)))