HTTP download file with Python


The urllib2 module can be used to download data from the web (network resource access). This data can be a file, a website or whatever you want Python to download. The module supports HTTP, HTTPS, FTP and several other protocols.

In this article you will learn how to download data from the web using Python.

Related courses

Download text

To download a plain text file use this code:

import urllib2
response = urllib2.urlopen('https://wordpress.org/plugins/about/readme.txt')
data = response.read()
print(data)

We get a response object using the urllib2.urlopen() method, where the parameter is the link. All of the file contents is received using the response.read() method call. After calling this, we have the file data in a Python variable of type string.

Download HTML

This will request the html code from a website. It will output everything to the screen.

import urllib2
response = urllib2.urlopen('http://en.wikipedia.org/')
html = response.read()
print html

Download file using Python

You can save the data to disk very easily after downloading the file:

import urllib2
response = urllib2.urlopen('https://wordpress.org/plugins/about/readme.txt')
data = response.read()
 
# Write data to file
filename = "test.txt"
file_ = open(filename, 'w')
file_.write(data)
file_.close()

The first part of the code downloads the file contents into the variable data:

import urllib2
response = urllib2.urlopen('https://wordpress.org/plugins/about/readme.txt')
data = response.read()

The second part stores it into a file (this file does not need to have the same filename)

# Write data to file
filename = "test.txt"
file_ = open(filename, 'w')
file_.write(data)
file_.close()

The ‘w’ parameter creates the file (or overwrites if it exists). You can read more about writing files here.

Back
Next

6 thoughts on “HTTP download file with Python

  1. Frank
    Samvid Goel - March 1, 2018

    Hi, I want to read a really big file (~400MB) from url. When I use requests.get(url) I get memory error. Can u suggest anyway of reading/downloading that file.

    1. Frank
      Frank - March 1, 2018

      In that case you could use sockets. Save every block to the disk immediately instead of holding it in memory.

  2. Frank
    Kharlo Cariaga - February 2, 2018

    Hi!, I’m new to using python as a web server, i’m having a very hard time on how to do this scenario.
    I’m Having a .html running from ElectronJS(app which runs .html files as .exe) in my local python server, and on a click of a button, it will generate a certain file, and it will be downloaded into the local machine(my pc), can you help me? Thank you very much!!

    1. Frank
      Frank - February 18, 2018

      Which Python server are you running? In the case of Flask or Django you can simply add it in the route.

  3. Frank
    Balaji - July 18, 2016

    Hi, I’m new to program! Could you help on how to download file using simple download button on website and python.
    I’ve a zip file at fixed location on server and I need a download option to save it on user’s laptop.

    1. Frank
      Frank - July 19, 2016

      Do you want this button to be in a graphical interface (like qt) ? Could you be more specific?

Leave a Reply