zoukankan      html  css  js  c++  java
  • requests实现文件下载, 期间显示文件信息&下载进度_python3

    requests实现文件下载, 期间显示文件信息&下载进度


    """使用模块线程方式实现网络资源的下载
    # 实现文件下载, 期间显示文件信息&下载进度
    # 控制台运行以显示进度
    """
    import requests
    import os.path as op
    import os
    from sys import stdout
    
    
    def downloadfile(url, filename):
        """下载文件并显示过程
        :param url: 资源地址
        :param filename: 保存的名字, 保存在当前目录
        """
        # print(url)
        filename = filename + '.' + op.splitext(url)[-1]
        file_to_save = op.join(os.getcwd(), filename)
        # print(file_to_save)
    
        with open(file_to_save, "wb") as fw:
            with requests.get(url, stream=True) as r:
                # 此时只有响应头被下载
                # print(r.headers)
                print("下载文件基本信息:")
                print('-' * 30)
                print("文件名称:", filename)
                print("文件类型:", r.headers["Content-Type"])
                filesize = r.headers["Content-Length"]
                print("文件大小:", filesize, "bytes")
                print("下载地址:", url)
                print("保存路径:", file_to_save)
                print('-' * 30)
                print("开始下载")
    
                chunk_size = 128
                times = int(filesize) // chunk_size
                show = 1 / times
                show2 = 1 / times
                start = 1
                for chunk in r.iter_content(chunk_size):
                    fw.write(chunk)
                    if start <= times:
                        stdout.write(f"下载进度: {show:.2%}
    ")
                        start += 1
                        show += show2
                    else:
                        stdout.write("下载进度: 100%")
                print("
    结束下载")
    
    
    if __name__ == "__main__":
        downloadfile("https://code.jquery.com/jquery-3.4.1.js", "a")
    

  • 相关阅读:
    Evanyou Blog 彩带
    Evanyou Blog 彩带
    Evanyou Blog 彩带
    Evanyou Blog 彩带
    Evanyou Blog 彩带
    Evanyou Blog 彩带
    Evanyou Blog 彩带
    Evanyou Blog 彩带
    Evanyou Blog 彩带
    Evanyou Blog 彩带
  • 原文地址:https://www.cnblogs.com/xust14521/p/11361338.html
Copyright © 2011-2022 走看看