Download excel file from internet 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") 

A file called test.xls did this, but it is clearly an html file. If I opened the html file in firefox, it opened the 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 above, how do I make python load the excel file as an excel file?

+6
source share
4 answers

This will save the excel file in the same folder from which the script was run.

 import urllib dls = "http://www.muellerindustries.com/uploads/pdf/UW SPD0114.xls" urllib.urlretrieve(dls, "test.xls") 
+3
source

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 established requests:

 pip install requests 
+9
source

To add a request (+1) to the Fedalto proposal, but make it more Pythonic with the 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) 
+5
source

Two questions: one with the code (below), the other is the wrong URL. A (modern) web browser will automatically fix " http://www.muellerindustries.com/uploads/pdf/UW SPD0114.xls" in " 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 a file.

+2
source

Source: https://habr.com/ru/post/974130/


All Articles