使用Python编写一个脚本,从指定的URL下载文件并保存到本地。
使用Python编写一个脚本,从指定的URL下载文件并保存到本地
import requests
# 指定要下载的文件的URL
url = 'https://www.example.com/file.zip'
# 发起GET请求
response = requests.get(url)
# 检查响应状态码
if response.status_code == 200:
# 获取文件名
file_name = url.split('/')[-1]
# 保存文件
with open(file_name, 'wb') as file:
file.write(response.content)
print('文件下载完成')
else:
print('文件下载失败')