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 requestsfile_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 wgetfile_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 requestsfile_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文件名

python url文件名_Python –从URL下载文件相关推荐

  1. 通过url地址批量打包zip下载文件

    通过url地址批量打包zip下载文件 controller @ApiOperation("通过下载url批量打包zip下载")@PostMapping("batchDow ...

  2. python下载文件加上日期_Python实现给下载文件显示进度条和下载时间代码

    本篇文章小编给大家分享一下Python实现给下载文件显示进度条和下载时间代码,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看. 该模块调用了三个库: 1. ...

  3. 如何用python批量下载数据_Python实现批量下载文件

    Python实现批量下载文件 #!/usr/bin/env python # -*- coding:utf-8 -*- from gevent import monkey monkey.patch_a ...

  4. 华为云OBS对象存储使用临时URL访问,并对下载文件重命名

    目录 解读 1.使用临时URL访问OBS,并获取临时URL 官方文档 举例 2.使用临时URL访问OBS,并对临时URL下载的文件重命名 分析 举例 解读 在使用OBS进行对云上文件进行下载的时候,发 ...

  5. python自动下载qq文件夹_Python实现批量下载文件

    Python实现批量下载文件 #!/usr/bin/env python # -*- coding:utf-8 -*- from gevent import monkey monkey.patch_a ...

  6. feign 使用示例:动态url、动态参数、下载文件、请求体

    文章目录 构建FeignClient接口实现 动态url 带参数的请求 @Param 带动态参数的请求 @QueryMap 下载文件的请求 带请求体的请求 @Body 官方使用文档: 链接 https ...

  7. python 打印文件名_Python | 打印文件内容以及文件名

    python 打印文件名 打印文件名 (Printing file name ) To print the filename, we use "file_object.name". ...

  8. python用 requests 模块从 Web 下载文件

    requests 模块让你很容易从 Web 下载文件,不必担心一些复杂的问题,诸如网络错误.连接问题和数据压缩.requests 模块不是 Python 自带的,所以必须先安装.通过命令行,运行 pi ...

  9. java 下载文件文件名_java – 从http下载文件中检索文件名

    我正在尝试从Web服务器下载文件并将其保存在服务器随文件发送的原始文件名下.下载工作得很好,但我无法获得真正的文件名. 举个实例我试图下载this file(Foxit PDF阅读器). 如果我在Ch ...

最新文章

  1. Python之MySQL数据库增删改查操作
  2. 参数binlog_row_image设置MINIMAL,你今天被坑了吗?
  3. 二维数组中的查找---剑指Offer
  4. 友盟的微信登录,QQ,新浪微博登录功能和分享功能
  5. 通过Java,Spring Boot应用程序将Gmail用作SMTP服务器
  6. UX2 beta 3正式发布!!
  7. C语言课后习题(65)
  8. selenium 定位方式5
  9. 1 元购买 1000+ 册爆款电子书,错过再等一年!
  10. 免费前端网站页面模板
  11. win10关闭某个端口
  12. 【天光学术】项目管理论文:房地产公司项目管理运营提升措施探究(节选)
  13. 开发中mock什么意思_开发中
  14. python怎么判断实数_检查python对象是否可以解释为实数[python]
  15. mysql 创建连接报错_创建数据库连接报错 · Issue #IRK5Z · 一米一粟信息科技/kooteam - Gitee.com...
  16. python中参数前面加星号是什么意思_详解Python函数中参数带星号是什么意思
  17. SNTP获取时间源统一时间
  18. 原型模式,也叫克隆、拷贝模式,深克隆浅克隆
  19. 2019华为实习面经
  20. Hdu2022 多校训练(5) BBQ

热门文章

  1. 前端试题-CSS试题(1)
  2. 一只火鸡带你了解大数据预测(经典)
  3. WAV文件的二进制格式解析
  4. [转载] python面面观单元测试_python 使用unittest进行单元测试
  5. [转载] python字符串情感分析_python进行情感分析
  6. [转载] java 中 date类型详解
  7. LeetCode-336 Palindrome Pairs
  8. Python 扩展知识:编程习惯
  9. tortoisegit 代码的回滚方式 --两种
  10. Jmeter中java接口测试