downloading an excel file from the web in python

后端 未结 4 1652
闹比i
闹比i 2020-12-05 09:00

I have the following web address:

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

I tried to download the file:

相关标签:
4条回答
  • 2020-12-05 09:03

    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)
    
    0 讨论(0)
  • 2020-12-05 09:22

    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
    
    0 讨论(0)
  • 2020-12-05 09:27

    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
    
    0 讨论(0)
  • 2020-12-05 09:27

    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.

    0 讨论(0)
提交回复
热议问题