def download_file(url, filename): import requests

response = requests.get(url, stream=True)  # 发送get请求,设置stream=True以便流式读取

with open(filename, "wb") as f:  # 以二进制写入模式打开文件
    for chunk in response.iter_content(chunk_size=1024):  # 以1024字节为块读取
        if chunk:  # 如果读取到了数据
            f.write(chunk)  # 将数据写入文件

调用示例

download_file("http://127.0.0.1/2.txt", "file.zip")

url = http1270012txt # 文件下载链接 filename = filezip # 文件保存的名字 response = requestsgeturl stream=True # 发送get请求设置stream=True以便流式读取 with openfilename wb as f # 以二进制写入模式打开文件 for chunk in

原文地址: https://www.cveoy.top/t/topic/dJm7 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录