Home  >  Article  >  Backend Development  >  Download files over HTTP in Python

Download files over HTTP in Python

WBOY
WBOYforward
2023-08-27 21:25:121207browse

In Python, we use Python built-in libraries such as urllib, request and httplib 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)

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()

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)

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()

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete