我有一个小工具,我用来从一个网站上下载一个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下载文件呢?
当前回答
你也可以通过urlretrieve得到进度反馈:
def report(blocknr, blocksize, size):
current = blocknr*blocksize
sys.stdout.write("\r{0:.2f}%".format(100.0*current/size))
def downloadFile(url):
print "\n",url
fname = url.split('/')[-1]
print fname
urllib.urlretrieve(url, fname, report)
其他回答
Python 3
urllib.request.urlopen 进口urllib.request Response = urllib.request.urlopen('http://www.example.com/') HTML = response.read() urllib.request.urlretrieve 进口urllib.request urllib.request.urlretrieve (' http://www.example.com/songs/mp3.mp3 ', ' mp3.mp3 ') 注意:根据文档,urllib.request.urlretrieve是一个“遗留接口”,并且“在未来可能会被弃用”(感谢gerrit)
Python 2
urllib2。urlopen(谢谢科里) 进口urllib2 Response = urllib2.urlopen('http://www.example.com/') HTML = response.read() urllib。urlretrieve(感谢PabloG) 进口urllib urllib.urlretrieve (' http://www.example.com/songs/mp3.mp3 ', ' mp3.mp3 ')
我同意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()
我写了下面的代码,它可以在普通的Python 2或Python 3中工作。
import sys
try:
import urllib.request
python3 = True
except ImportError:
import urllib2
python3 = False
def progress_callback_simple(downloaded,total):
sys.stdout.write(
"\r" +
(len(str(total))-len(str(downloaded)))*" " + str(downloaded) + "/%d"%total +
" [%3.2f%%]"%(100.0*float(downloaded)/float(total))
)
sys.stdout.flush()
def download(srcurl, dstfilepath, progress_callback=None, block_size=8192):
def _download_helper(response, out_file, file_size):
if progress_callback!=None: progress_callback(0,file_size)
if block_size == None:
buffer = response.read()
out_file.write(buffer)
if progress_callback!=None: progress_callback(file_size,file_size)
else:
file_size_dl = 0
while True:
buffer = response.read(block_size)
if not buffer: break
file_size_dl += len(buffer)
out_file.write(buffer)
if progress_callback!=None: progress_callback(file_size_dl,file_size)
with open(dstfilepath,"wb") as out_file:
if python3:
with urllib.request.urlopen(srcurl) as response:
file_size = int(response.getheader("Content-Length"))
_download_helper(response,out_file,file_size)
else:
response = urllib2.urlopen(srcurl)
meta = response.info()
file_size = int(meta.getheaders("Content-Length")[0])
_download_helper(response,out_file,file_size)
import traceback
try:
download(
"https://geometrian.com/data/programming/projects/glLib/glLib%20Reloaded%200.5.9/0.5.9.zip",
"output.zip",
progress_callback_simple
)
except:
traceback.print_exc()
input()
注:
支持“进度条”回调。 从我的网站上下载一个4 MB的测试包。zip。
另一种方法是调用外部进程,如curl.exe。Curl在默认情况下显示进度条、平均下载速度、剩余时间等,所有这些都整齐地格式化在一个表格中。 将curl.exe放在与脚本相同的目录中
from subprocess import call
url = ""
call(["curl", {url}, '--output', "song.mp3"])
注意:不能使用curl指定输出路径,所以使用os。重命名之后
使用wget模块:
import wget
wget.download('url')