写一个python脚本自动下载软件
时间: 2024-09-15 20:02:28 浏览: 52
在Python中编写一个自动下载软件的脚本通常涉及到网络请求、文件操作和可能需要处理一些依赖库。下面是一个简单的例子,我们将使用requests库下载一个网页上的文件:
```python
import os
import requests
def download_file(url, save_path):
# 检查目标路径是否存在,不存在则创建
if not os.path.exists(os.path.dirname(save_path)):
os.makedirs(os.path.dirname(save_path))
# 发起GET请求
response = requests.get(url, stream=True)
# 检查请求是否成功
if response.status_code == 200:
# 开始写入文件
with open(save_path, 'wb') as file:
for chunk in response.iter_content(chunk_size=8192):
if chunk: # filter out keep-alive new chunks
file.write(chunk)
print(f"文件 {save_path} 已成功下载")
else:
print(f"请求失败,状态码:{response.status_code}")
# 使用函数下载特定的软件URL
software_url = "https://example.com/software.zip"
download_path = "/path/to/save/downloaded_software.zip"
download_file(software_url, download_path)
阅读全文
相关推荐

















