用python从网上下载一个excel文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25415405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
downloading an excel file from the web in python
提问by zelinka
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.
这生成了一个名为“test.xls”的文件,但这显然是一个 html 文件。如果我在 firefox 中打开 html 文件,它会打开一个 excel 文件,但如果我在 excel 中打开文件,它绝对不是我要找的 excel 文件。
If I have a web address like the one above, how do I make python download the excel file as an excel file?
如果我有一个像上面这样的网址,我如何让python将excel文件下载为excel文件?
采纳答案by mnjeremiah
This would save the excel file in the same folder that the script was ran from.
这会将 excel 文件保存在运行脚本的同一文件夹中。
import urllib
dls = "http://www.muellerindustries.com/uploads/pdf/UW SPD0114.xls"
urllib.urlretrieve(dls, "test.xls")
回答by Fedalto
回答by BKay
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/UWSPD0114.xls" to "http://www.muellerindustries.com/uploads/pdf/UW%20SPD0114.xls" but Python doesn't.
两个问题,一个是代码(如下),另一个是 URL 错误。(现代)网络浏览器会自动将“ http://www.muellerindustries.com/uploads/pdf/UWSPD0114.xls”更正为“ http://www.muellerindustries.com/uploads/pdf/UW%20SPD0114.xls"但 Python 没有。
This code works for me on python 3.x
这段代码在 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.
这让我得到了文件。
回答by Aaron Hall
To add on to Fedalto's requests suggestion (+1), but make it more Pythonic with a context manager:
添加 Fedalto 的请求建议 (+1),但使用上下文管理器使其更加 Pythonic:
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)

