Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

downloading an excel file from the web in python

I have the following web address:

dls = "http://www.muellerindustries.com/uploads/pdf/UW SPD0114.xls"

I tried to download the file:

urllib2.urlopen(dls, "test.xls")

This made a file called "test.xls" but this is clearly an html file. If I opened the html file in firefox it opened an excel file, but if I opened the file in excel it was definitely not the excel file I was looking for.

If I have a web address like the one above, how do I make python download the excel file as an excel file?

like image 951
zelinka Avatar asked Aug 20 '14 22:08

zelinka


People also ask

Can I use Python to download files from website?

Requests is a versatile HTTP library in python with various applications. One of its applications is to download a file from web using the file URL.


4 Answers

I suggest using requests:

import requests
dls = "http://www.muellerindustries.com/uploads/pdf/UW SPD0114.xls"
resp = requests.get(dls)

output = open('test.xls', 'wb')
output.write(resp.content)
output.close()

To get requests installed:

pip install requests
like image 191
Fedalto Avatar answered Sep 21 '22 20:09

Fedalto


To add on to Fedalto's requests suggestion (+1), but make it more Pythonic with a context manager:

import requests
dls = "http://www.muellerindustries.com/uploads/pdf/UW SPD0114.xls"
resp = requests.get(dls)
with open('test.xls', 'wb') as output:
    output.write(resp.content)
like image 35
Russia Must Remove Putin Avatar answered Sep 21 '22 20:09

Russia Must Remove Putin


This would save the excel file in the same folder that the script was ran from.

import urllib
dls = "http://www.muellerindustries.com/uploads/pdf/UW SPD0114.xls"
urllib.request.urlretrieve(dls, "test.xls")  # For Python 3
# urllib.urlretrieve(dls, "test.xls")  # For Python 2
like image 21
mnjeremiah Avatar answered Sep 21 '22 20:09

mnjeremiah


Two issues, one with the code (below), the other that the URL is bad. A (modern) web browser will automatically correct "http://www.muellerindustries.com/uploads/pdf/UW SPD0114.xls" to "http://www.muellerindustries.com/uploads/pdf/UW%20SPD0114.xls" but Python doesn't.

This code works for me on python 3.x

import urllib
outfilename = "test.xls"
url_of_file = "http://www.muellerindustries.com/uploads/pdf/UW%20SPD0114.xls"
urllib.request.urlretrieve(url_of_file, outfilename) 

Which gets me the file.

like image 32
BKay Avatar answered Sep 22 '22 20:09

BKay