python url文件名_Python –从URL下载文件

本文介绍了使用Python从URL下载文件的各种方法,包括使用requests库和wget库。针对重定向的URL,requests库能自动跟随下载。对于大文件,可以使用分块下载以避免内存问题。教程还提供了相关资源和示例代码。

python url文件名

Downloading a File from URL is a very common task in Python scripts. A real life example is to download images from a website to the local system and then process it in our Python program.

从URL下载文件是Python脚本中非常常见的任务。 一个真实的例子是将图像从网站下载到本地系统,然后在我们的Python程序中进行处理。

In this tutorial, we will learn different ways to download file from a URL in Python.

在本教程中,我们将学习使用Python从URL下载文件的不同方法。

使用请求库从Python脚本中的URL下载文件 (Using requests library to download file from URL in Python Scripts)

If your requirement is to get the file from a given URL using GET HTTP request, then the Python requests module is perfect for you.

如果您的要求是使用GET HTTP请求从给定的URL获取文件,那么Python requests模块非常适合您。


import requests

file_url = 'https://www.journaldev.com/wp-content/uploads/2019/08/Python-Tutorial.png'

file_object = requests.get(file_url)

with open('Python-Tutorial.png', 'wb') as local_file:
    local_file.write(file_object.content)

The file will be downloaded in the same directory as the Python script. If you want to change the directory location, you can provide a complete path or relative path in the open() function call.

该文件将下载到与Python脚本相同的目录中。 如果要更改目录位置,可以在open()函数调用中提供完整路径或相对路径。

Recommended Reading: Python with Statement

推荐读物Python with Statement

Linux迷? 使用Python wget库从URL下载文件 (Linux aficionado? Use Python wget library to download file from URL)

If you love Linux commands and want to have similar flavor in your Python program, you can use wget library to download the file from a URL.

如果您喜欢Linux命令并且希望在Python程序中具有类似的风格,则可以使用wget库从URL下载文件。

Python wget library is not part of the default installation, so you can install it using the PIP package manager.

Python wget库不是默认安装的一部分,因此您可以使用PIP软件包管理器进行安装。


# pip install wget

Here is the Python program to download a file from URL using wget library.

这是使用wget库从URL下载文件的Python程序。


import wget

file_url = 'https://www.journaldev.com/wp-content/uploads/2019/08/Python-Tutorial.png'

dest_file = '/Users/pankaj/pt.png'

wget.download(file_url, dest_file)

The destination file argument is optional. If we don’t provide that then the file will be saved in the same directory as the script and filename will be the same as the remote file name.

目标文件参数是可选的。 如果我们不提供该文件,则文件将与脚本保存在同一目录中,文件名将与远程文件名相同。

从重定向的URL下载文件 (Downloading File from a URL that Redirects)

Sometimes we get short URLs that redirect to the actual file. The requests library get() method automatically follows the redirect and download the actual file. If you look at the get() implementation, it sets allow_redirects parameter as True.

有时,我们会获得简短的URL,这些URL会重定向到实际文件。 请求库的get()方法自动遵循重定向并下载实际文件。 如果查看get()实现,它将allow_redirects参数设置为True


def get(url, params=None, **kwargs):
    kwargs.setdefault('allow_redirects', True)
    return request('get', url, params=params, **kwargs)

从Python中的URL下载大文件 (Downloading a Large File from URL in Python)

If the file is large, then it’s not a good idea to get all the content in one go. It will require a lot of memory and might cause out of memory error.

如果文件很大,那么一次性获取所有内容不是一个好主意。 这将需要大量内存,并可能导致内存不足错误。

We can pass stream=True to requests get() method to open a file stream and download it in chunks. Then we can use a for loop to read the chunks and write it into the local file.

我们可以将stream=True传递给请求的get()方法,以打开文件流并分块下载。 然后,我们可以使用for循环读取块并将其写入本地文件。


import requests

file_url = 'https://www.journaldev.com/wp-content/uploads/2019/08/Python-Tutorial.png'

file_stream = requests.get(file_url, stream=True)

with open('Python-Tutorial.png', 'wb') as local_file:
    for data in file_stream:
        local_file.write(data)

print('Done')

结论 (Conclusion)

It’s very easy to download a file from URL in Python. The requests module is perfect for it. We can also specify the HTTP methods to download the file.

从Python中的URL下载文件非常容易。 请求模块非常适合它。 我们还可以指定HTTP方法来下载文件。

Reference: Requests Module Official Docs

参考请求模块正式文档

翻译自: https://www.journaldev.com/39379/python-download-file-from-url

python url文件名

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值