python中下载文件的库和函数有很多,在写爬虫下载文件时推荐使用requests库
安装
pip install requests
下载文件
import requests
url = 'https://example.com/file.txt'
file_name = 'file.txt'
# 发起 GET 请求并下载文件
response = requests.get(url)
# 保存文件
with open(file_name, 'wb') as f:
f.write(response.content)
print(f"{file_name} 下载完成!")
Python爬虫中使用requests下载文件
1120

被折叠的 条评论
为什么被折叠?



