Download files over HTTP in Python

WBOY
Release: 2023-08-27 21:25:12
forward
1198 people have browsed it

In Python, we use Python built-in libraries such asurllib, requestandhttplib to download files over HTTP. HTTP is a Hypertext Transfer Protocol used to access data over the World Wide Web. HTTP requests are typically initiated by a client, such as a web browser, and sent to the server hosting the requested resource. Requests typically include a method (such as GET or POST), a Uniform Resource Locator (URL) that identifies the resource, and optional headers that provide additional information about the request. In this article, we will learn how to download files using urllib and request libraries.

Use urllib to download files

Urllib contains submodules such as urllib.request, which can be used to easily download files from the Internet. urllib.request takes as input the URL of the file and the filename you want to provide for the downloaded file.

grammar

urllib.request.urlretrieve(URL, filename)
Copy after login

Hereurllib.request.urlretrieveThere are two parameters. One is the URL of the file on the internet and the other is the filename you want to give the downloaded file.

Example

In the following example, the urllib library is used to download files over HTTP, first importing the urllib.request module, and then calling the urlretrive function of the urllib.request module. Pass the URL of the file to download and the file name to retain after downloading the file.

If an invalid URL is passed or the file is not downloadable, an exception will be thrown.

import urllib.request from PIL import Image url = 'https://www.python.org/static/img/python-logo.png' filename = 'python-logo.png' urllib.request.urlretrieve(url, filename) with open(filename, 'rb') as f: image = Image.open(f) image.show()
Copy after login

Output

Download files over HTTP in Python

Use requests library to download files

requests library makes HTTP requests in Python using its get method. It simply takes the URL of the file as input, makes a get request to download the file, and returns the downloaded file as a response.

grammar

requests.get(URL)
Copy after login

Hererequests.get()The URL in the method is the URL of the file to be downloaded through the Internet.

Example

In the following example, we import the requests library and specify the URL of the file to download and the name to give the downloaded file. We then import the requests library and use the requests.get() method to download the Python logo. This method returns a response object containing the contents of the file. Finally, we read the downloaded file and print it on the screen.

import requests url = 'https://www.python.org/static/img/python-logo.png' filename = 'python-logo.png' response = requests.get(url) from PIL import Image with open(filename, 'rb') as f: image = Image.open(f) image.show()
Copy after login

Output

Download files over HTTP in Python

in conclusion

In this article, we discussed how to download files over HTTP in Python using Python built-in libraries such as urllib and requests library. The requests library provides a higher-level interface that is more user-friendly than urllib. The requests library provides a simpler way to download files than the urllib library. Any library can be used to download files in Python.

The above is the detailed content of Download files over HTTP in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!