我有一个小工具,我用来从一个网站上下载一个MP3文件,然后构建/更新一个播客XML文件,我已经添加到iTunes。
创建/更新XML文件的文本处理是用Python编写的。但是,我在Windows .bat文件中使用wget来下载实际的MP3文件。我更喜欢用Python编写整个实用程序。
我努力寻找一种用Python实际下载该文件的方法,因此我使用了wget。
那么,如何使用Python下载文件呢?
我有一个小工具,我用来从一个网站上下载一个MP3文件,然后构建/更新一个播客XML文件,我已经添加到iTunes。
创建/更新XML文件的文本处理是用Python编写的。但是,我在Windows .bat文件中使用wget来下载实际的MP3文件。我更喜欢用Python编写整个实用程序。
我努力寻找一种用Python实际下载该文件的方法,因此我使用了wget。
那么,如何使用Python下载文件呢?
当前回答
import urllib2
mp3file = urllib2.urlopen("http://www.example.com/songs/mp3.mp3")
with open('test.mp3','wb') as output:
output.write(mp3file.read())
open('test.mp3','wb')中的wb以二进制模式打开文件(并擦除任何现有文件),以便您可以使用它保存数据而不仅仅是文本。
其他回答
在python3中,可以使用urllib3和shutil库。 使用pip或pip3下载它们(取决于python3是否默认)
pip3 install urllib3 shutil
然后运行这段代码
import urllib.request
import shutil
url = "http://www.somewebsite.com/something.pdf"
output_file = "save_this_name.pdf"
with urllib.request.urlopen(url) as response, open(output_file, 'wb') as out_file:
shutil.copyfileobj(response, out_file)
注意,您下载了urllib3,但在代码中使用了urllib
使用urllib.request.urlopen ():
import urllib.request
with urllib.request.urlopen('http://www.example.com/') as f:
html = f.read().decode('utf-8')
这是使用库的最基本的方法,没有任何错误处理。您还可以执行更复杂的操作,例如更改头文件。
在Python 2中,该方法在urllib2中:
import urllib2
response = urllib2.urlopen('http://www.example.com/')
html = response.read()
我同意Corey的观点,urllib2比urllib更完整,如果你想做更复杂的事情,应该使用urllib2模块,但为了让答案更完整,如果你只想要基本的东西,urllib是一个更简单的模块:
import urllib
response = urllib.urlopen('http://www.example.com/sound.mp3')
mp3 = response.read()
会很好。或者,如果你不想处理"response"对象,你可以直接调用read():
import urllib
mp3 = urllib.urlopen('http://www.example.com/sound.mp3').read()
如果速度对你来说很重要,我为urllib和wget模块做了一个小的性能测试,关于wget,我尝试了一次状态栏和一次没有状态栏。我使用了三个不同的500MB文件进行测试(不同的文件-以消除在底层进行缓存的可能性)。在debian机器上测试,使用python2。
首先,这些是结果(它们在不同的运行中是相似的):
$ python wget_test.py
urlretrive_test : starting
urlretrive_test : 6.56
==============
wget_no_bar_test : starting
wget_no_bar_test : 7.20
==============
wget_with_bar_test : starting
100% [......................................................................] 541335552 / 541335552
wget_with_bar_test : 50.49
==============
我执行测试的方式是使用“profile”装饰器。这是完整的代码:
import wget
import urllib
import time
from functools import wraps
def profile(func):
@wraps(func)
def inner(*args):
print func.__name__, ": starting"
start = time.time()
ret = func(*args)
end = time.time()
print func.__name__, ": {:.2f}".format(end - start)
return ret
return inner
url1 = 'http://host.com/500a.iso'
url2 = 'http://host.com/500b.iso'
url3 = 'http://host.com/500c.iso'
def do_nothing(*args):
pass
@profile
def urlretrive_test(url):
return urllib.urlretrieve(url)
@profile
def wget_no_bar_test(url):
return wget.download(url, out='/tmp/', bar=do_nothing)
@profile
def wget_with_bar_test(url):
return wget.download(url, out='/tmp/')
urlretrive_test(url1)
print '=============='
time.sleep(1)
wget_no_bar_test(url2)
print '=============='
time.sleep(1)
wget_with_bar_test(url3)
print '=============='
time.sleep(1)
Urllib似乎是最快的
使用wget模块:
import wget
wget.download('url')