我有一个小工具,我用来从一个网站上下载一个MP3文件,然后构建/更新一个播客XML文件,我已经添加到iTunes。

创建/更新XML文件的文本处理是用Python编写的。但是,我在Windows .bat文件中使用wget来下载实际的MP3文件。我更喜欢用Python编写整个实用程序。

我努力寻找一种用Python实际下载该文件的方法,因此我使用了wget。

那么,如何使用Python下载文件呢?


当前回答

如果速度对你来说很重要,我为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似乎是最快的

其他回答

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 ')

还有一个,使用urlretrieve:

import urllib.request
urllib.request.urlretrieve("http://www.example.com/songs/mp3.mp3", "mp3.mp3")

(对于Python 2使用import urllib和urllib.urlretrieve)

你可以使用keras.utils。Get_file来做它:

from tensorflow import keras

path_to_downloaded_file = keras.utils.get_file(
    fname="file name",
    origin="https://www.linktofile.com/link/to/file",
    extract=True,
    archive_format="zip",  # downloaded file format
    cache_dir="/",  # cache and extract in current directory
)

这可能有点晚了,但我看到了pabloG的代码,忍不住添加了一个os.system('cls'),使它看起来很棒!看看吧:

    import urllib2,os

    url = "http://download.thinkbroadband.com/10MB.zip"

    file_name = url.split('/')[-1]
    u = urllib2.urlopen(url)
    f = open(file_name, 'wb')
    meta = u.info()
    file_size = int(meta.getheaders("Content-Length")[0])
    print "Downloading: %s Bytes: %s" % (file_name, file_size)
    os.system('cls')
    file_size_dl = 0
    block_sz = 8192
    while True:
        buffer = u.read(block_sz)
        if not buffer:
            break

        file_size_dl += len(buffer)
        f.write(buffer)
        status = r"%10d  [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
        status = status + chr(8)*(len(status)+1)
        print status,

    f.close()

如果在Windows以外的环境中运行,你必须使用'cls'以外的东西。在MAC OS X和Linux中,它应该是“清晰的”。

使用wget模块:

import wget
wget.download('url')