python logo

python download file from url

The urllib2 module in Python is a versatile tool for downloading files from the internet. It’s an integral part of Python’s network resource access capability and supports multiple protocols such as HTTP, HTTPS, and FTP.

If you’re keen on automating web-based tasks or downloading files from websites, understanding the urllib2 module is essential. This guide will demonstrate how you can fetch data from the internet using Python.

Browser Automation with Python Selenium

How to Download Text Files with Python

If you aim to download plain text files like .txt, the code snippet below will serve the purpose:

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

Here’s a breakdown of the code:

  1. We use the urllib2.urlopen() method to initiate a connection to the URL provided.
  2. The entire content of the file is fetched using the response.read() method. This data is then stored in a Python string variable.

Downloading HTML Content

To fetch the HTML content of a website, you can use the following code. This will print the entire HTML structure of the given webpage:

1
2
3
4
import urllib2
response = urllib2.urlopen('https://en.wikipedia.org/')
html = response.read()
print html

Saving Downloaded Files using Python

Once you’ve fetched the data, saving it to your local disk is straightforward. The following code snippet demonstrates this:

1
2
3
4
5
6
7
8
9
import urllib2
response = urllib2.urlopen('https://wordpress.org/plugins/about/readme.txt')
data = response.read()

# Save the downloaded data to a file
filename = "test.txt"
file_ = open(filename, 'w')
file_.write(data)
file_.close()

A brief explanation:

  • Initially, the file content is downloaded and stored in the data variable.
  • Next, we choose a filename (in this case “test.txt”) and open the file in write mode ('w'). This mode will create the file if it doesn’t exist or overwrite it if it does.
  • The data is then written to the file and subsequently closed.

Want to learn more about file operations in Python? Discover the intricacies of writing files in Python.

Browser Automation with Python Selenium

Previous Tutorial | Next Tutorial