python 手动给requests模块添加urlretrieve下载文件方法!

这篇博客介绍了如何在requests模块中实现类似urllib.request.urlretrieve的功能,以简化文件下载过程。作者通过分析urllib模块,理解了下载文件的本质,并创建了一个自定义的urlretrieve方法,利用requests的iter_content()函数进行文件段落写入。最后,作者将该方法整合到requests模块中,方便使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

requests模块的前代是urllib模块,传入参数headers、cookie、data什么的肯定是requests好使,但是却没有urllib.request.urlretrieve这个方法,

urlretrieve(url, filename=None,reporthook=None, params=None,)

传入url跟文件路径即可下载文件,requests次次都得自己手动编写,我觉得太麻烦了,而且它还有个回调函数,我试着能不能把这个urlretrieve方法移植到requests模块来

要点:1.如何找到自己想要的python模块呢?cmd上打path,找出python的,然后CTRL+F

         2.下载文件的实质是 contextlib.closing打开网页--->with open文件--->写入

     3.reporthook回调函数的实质就是 把文件一段段写入文件时把3个参数(每次写入bytes的数量、次数、headers得到的总大小size)传出去,让回调函数处理

    4.原来现在的模块,都是其他py文件写好方法,然后把其方法传入__init__.py这个文件的

    5.r.iter_content()的应用


进入urllib文件夹,在request文件中找到urlretrieve方法,具体如下


def urlretrieve(url, filename=None, reporthook=None, data=None):
    """
    Retrieve a URL into a temporary location on disk.

    Requires a URL argument. If a filename is passed, it is used as
    the temporary file location. The reporthook argument should be
    a callable that accepts a block number, a read size, and the
    total file size of the URL target. The data argument should be
    valid URL encoded data.

    If a filename is passed and the URL points to a local resource,
    the result is a copy from local file to new file.

    Returns a tuple containing the path to the newly created
    data file as well as the resulting HTTPMessage object.
    """
    url_type, path = splittype(url)       #分析网页的,忽略

    with contextlib.closing(urlopen(url, data)) as fp: #打开网页
        headers = fp.info()              #头

        # Just return the local path and the "headers" for file://
        # URLs. No sense in performing a copy unless requested.
        if url_type == "file" and not filename:
            return os.path.normpath(path), headers #忽略

        # Handle temporary file setup.
        if filename
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值