【问题标题】:How to download a file over HTTP?如何通过 HTTP 下载文件?
【发布时间】:2010-09-06 13:34:15
【问题描述】:

我有一个小实用程序,用于按计划从网站下载 MP3 文件,然后构建/更新已添加到 iTunes 的播客 XML 文件。

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

我很难找到一种在 Python 中实际下载文件的方法,因此我使用了wget

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

【问题讨论】:

  • 下面的许多答案都不能令人满意地替代wget。除其他外,wget (1) 保留时间戳 (2) 从 url 自动确定文件名,如果文件已存在,则附加 .1 (等) (3) 有许多其他选项,其中一些您可能已经放置在你的.wgetrc。如果你想要其中任何一个,你必须自己在 Python 中实现它们,但从 Python 调用 wget 会更简单。
  • Python 3 的简短解决方案:import urllib.request; s = urllib.request.urlopen('http://example.com/').read().decode()

标签: python http urllib


【解决方案1】:

新的基于 Api urllib3 的实现

>>> import urllib3
>>> http = urllib3.PoolManager()
>>> r = http.request('GET', 'your_url_goes_here')
>>> r.status
   200
>>> r.data
   *****Response Data****

更多信息:https://pypi.org/project/urllib3/

【讨论】:

    【解决方案2】:

    还有一个,使用urlretrieve

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

    (对于 Python 3+,请使用 import urllib.requesturllib.request.urlretrieve

    还有一个,带有“进度条”

    import urllib2
    
    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)
    
    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()
    

    【讨论】:

    • 奇怪的是,当 urllib2 方法不起作用时,这在 Windows 上对我有用。不过,urllib2 方法在 Mac 上也有效。
    • 错误:file_size_dl += block_sz 应该是 += len(buffer),因为最后一次读取通常不是完整的 block_sz。同样在 Windows 上,如果输出文件不是文本文件,则需要将其打开为“wb”。
    • 我也是 urllib 和 urllib2 没有工作,但 urlretrieve 工作得很好,很沮丧 - 谢谢 :)
    • if not os.path.isfile(file_name): 包裹整个内容(除了文件名的定义)以避免覆盖播客!当使用 .html 文件中的 url 将其作为 cronjob 运行时很有用
    • 根据文档,urllib.request.urlretrieve 是一个“旧接口”并且“将来可能会被弃用。docs.python.org/3/library/urllib.request.html#legacy-interface
    【解决方案3】:

    另一种方法是调用外部进程,例如 curl.exe。 Curl 默认显示进度条、平均下载速度、剩余时间等,所有这些都整齐地排列在表格中。 将 curl.exe 与您的脚本放在同一目录中

    from subprocess import call
    url = ""
    call(["curl", {url}, '--output', "song.mp3"])
    

    注意:你不能用 curl 指定输出路径,所以之后要进行 os.rename

    【讨论】:

      【解决方案4】:

      使用wget module:

      import wget
      wget.download('url')
      

      【讨论】:

      • 回购似乎已被删除。
      • 项目已移至github,但随后被作者归档
      【解决方案5】:

      使用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()
      

      【讨论】:

      • 如果您提供的网址中有空格,这将不起作用。在这种情况下,您需要解析 url 并对路径进行 urlencode。
      • 这里是 Python 3 解决方案:stackoverflow.com/questions/7243750/…
      • 仅供参考。 url编码路径的方式是urllib2.quote
      • @JasonSundram:如果里面有空格,那就不是URI。
      • 这不适用于文件较大的窗口。您需要阅读所有块!
      【解决方案6】:

      Python 3

      • urllib.request.urlopen

        import urllib.request
        response = urllib.request.urlopen('http://www.example.com/')
        html = response.read()
        
      • urllib.request.urlretrieve

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

        注意:根据文档,urllib.request.urlretrieve 是“旧接口”并且“将来可能会被弃用”(感谢gerrit

      Python 2

      • urllib2.urlopen(感谢Corey

        import urllib2
        response = urllib2.urlopen('http://www.example.com/')
        html = response.read()
        
      • urllib.urlretrieve(感谢PabloG

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

      【讨论】:

      • 确实花了一些时间,但终于有了我期望从 python stdlib 获得的简单直接的 api :)
      • python3 的非常好的答案,另见docs.python.org/3/library/…
      • @EdouardThiel 如果你点击上面的urllib.request.urlretrieve,它会带你到那个确切的链接。干杯!
      • urllib.request.urlretrieve 被记录为“旧接口”并且“将来可能会被弃用”。
      • 你应该提到你得到了一堆需要处理的字节。
      【解决方案7】:

      2012年,使用python requests library

      >>> import requests
      >>> 
      >>> url = "http://download.thinkbroadband.com/10MB.zip"
      >>> r = requests.get(url)
      >>> print len(r.content)
      10485760
      

      您可以运行pip install requests 来获取它。

      Requests 与其他替代方案相比具有许多优势,因为 API 更简单。如果您必须进行身份验证,则尤其如此。 urllib 和 urllib2 在这种情况下非常不直观和痛苦。


      2015-12-30

      人们对进度条表示钦佩。这很酷,当然。现在有几种现成的解决方案,包括tqdm

      from tqdm import tqdm
      import requests
      
      url = "http://download.thinkbroadband.com/10MB.zip"
      response = requests.get(url, stream=True)
      
      with open("10MB", "wb") as handle:
          for data in tqdm(response.iter_content()):
              handle.write(data)
      

      这本质上是 @kvance 30 个月前描述的实现。

      【讨论】:

      • 如何处理大文件,是否所有内容都存储到内存中,或者可以将其写入文件而无需大内存需求?
      • 可以通过在请求中设置stream=True来流式传输大文件。然后,您可以在响应上调用 iter_content() 以一次读取一个块。
      • 为什么一个 url 库需要一个文件解压工具?从 url 读取文件,保存它,然后以任何方式解压缩它。此外,zip 文件不是 Windows 中显示的“文件夹”,而是一个文件。
      • @Ali:r.text:用于文本或 unicode 内容。以 Unicode 形式返回。 r.content:用于二进制内容。作为字节返回。在这里阅读:docs.python-requests.org/en/latest/user/quickstart
      • 我认为chunk_size 参数与stream=True 一起是可取的。默认chunk_size1,也就是说,每个chunk可以小到1字节,效率很低。
      【解决方案8】:

      我想从网页下载所有文件。我尝试了wget,但它失败了,所以我决定使用 Python 路由并找到了这个线程。

      阅读后,我做了一个小命令行应用程序soupget,扩展了PabloGStan 的优秀答案,并添加了一些有用的选项。

      它使用BeatifulSoup 收集页面的所有 URL,然后下载具有所需扩展名的 URL。最后它可以并行下载多个文件。

      这里是:

      #!/usr/bin/env python3
      # -*- coding: utf-8 -*-
      from __future__ import (division, absolute_import, print_function, unicode_literals)
      import sys, os, argparse
      from bs4 import BeautifulSoup
      
      # --- insert Stan's script here ---
      # if sys.version_info >= (3,): 
      #...
      #...
      # def download_file(url, dest=None): 
      #...
      #...
      
      # --- new stuff ---
      def collect_all_url(page_url, extensions):
          """
          Recovers all links in page_url checking for all the desired extensions
          """
          conn = urllib2.urlopen(page_url)
          html = conn.read()
          soup = BeautifulSoup(html, 'lxml')
          links = soup.find_all('a')
      
          results = []    
          for tag in links:
              link = tag.get('href', None)
              if link is not None: 
                  for e in extensions:
                      if e in link:
                          # Fallback for badly defined links
                          # checks for missing scheme or netloc
                          if bool(urlparse.urlparse(link).scheme) and bool(urlparse.urlparse(link).netloc):
                              results.append(link)
                          else:
                              new_url=urlparse.urljoin(page_url,link)                        
                              results.append(new_url)
          return results
      
      if __name__ == "__main__":  # Only run if this file is called directly
          # Command line arguments
          parser = argparse.ArgumentParser(
              description='Download all files from a webpage.')
          parser.add_argument(
              '-u', '--url', 
              help='Page url to request')
          parser.add_argument(
              '-e', '--ext', 
              nargs='+',
              help='Extension(s) to find')    
          parser.add_argument(
              '-d', '--dest', 
              default=None,
              help='Destination where to save the files')
          parser.add_argument(
              '-p', '--par', 
              action='store_true', default=False, 
              help="Turns on parallel download")
          args = parser.parse_args()
      
          # Recover files to download
          all_links = collect_all_url(args.url, args.ext)
      
          # Download
          if not args.par:
              for l in all_links:
                  try:
                      filename = download_file(l, args.dest)
                      print(l)
                  except Exception as e:
                      print("Error while downloading: {}".format(e))
          else:
              from multiprocessing.pool import ThreadPool
              results = ThreadPool(10).imap_unordered(
                  lambda x: download_file(x, args.dest), all_links)
              for p in results:
                  print(p)
      

      其用法示例如下:

      python3 soupget.py -p -e <list of extensions> -d <destination_folder> -u <target_webpage>
      

      如果你想看到它的实际效果,还有一个实际的例子:

      python3 soupget.py -p -e .xlsx .pdf .csv -u https://healthdata.gov/dataset/chemicals-cosmetics
      

      【讨论】:

        【解决方案9】:

        迟到的答案,但对于python&gt;=3.6,您可以使用:

        import dload
        dload.save(url)
        

        安装dload

        pip3 install dload
        

        【讨论】:

        • 请问 - 程序运行后文件保存在哪里?另外,有没有办法命名它并将其保存在特定位置?这是我正在使用的链接 - 当您单击该链接时,它会立即下载一个 Excel 文件:ons.gov.uk/generator?format=xls&uri=/economy/…
        • 您可以提供保存位置作为第二个参数,例如:dload.save(url, "/home/user/test.xls")
        【解决方案10】:

        urlretrieve 和 requests.get 很简单,但事实并非如此。 我已经为几个站点获取了数据,包括文本和图像,以上两个可能解决了大部分任务。但对于更通用的解决方案,我建议使用 urlopen。由于它包含在 Python 3 标准库中,因此您的代码可以在任何运行 Python 3 的机器上运行,而无需预先安装站点包

        import urllib.request
        url_request = urllib.request.Request(url, headers=headers)
        url_connect = urllib.request.urlopen(url_request)
        
        #remember to open file in bytes mode
        with open(filename, 'wb') as f:
            while True:
                buffer = url_connect.read(buffer_size)
                if not buffer: break
        
                #an integer value of size of written data
                data_wrote = f.write(buffer)
        
        #you could probably use with-open-as manner
        url_connect.close()
        

        当使用 Python 通过 http 下载文件时,此答案提供了 HTTP 403 Forbidden 的解决方案。我只尝试了 requests 和 urllib 模块,其他模块可能会提供更好的东西,但这是我用来解决大部分问题的模块。

        【讨论】:

          【解决方案11】:

          您可以在 Python 2 和 3 上使用 PycURL

          import pycurl
          
          FILE_DEST = 'pycurl.html'
          FILE_SRC = 'http://pycurl.io/'
          
          with open(FILE_DEST, 'wb') as f:
              c = pycurl.Curl()
              c.setopt(c.URL, FILE_SRC)
              c.setopt(c.WRITEDATA, f)
              c.perform()
              c.close()
          

          【讨论】:

            【解决方案12】:

            为了完整起见,也可以使用subprocess 包调用任何程序来检索文件。专门用于检索文件的程序比 urlretrieve 等 Python 函数更强大。例如wget可以递归下载目录(-R),可以处理FTP、重定向、HTTP代理,可以避免重新下载现有文件(-nc),aria2可以进行多连接下载可能会加快您的下载速度。

            import subprocess
            subprocess.check_output(['wget', '-O', 'example_output_file.html', 'https://example.com'])
            

            在 Jupyter Notebook 中,也可以使用! 语法直接调用程序:

            !wget -O example_output_file.html https://example.com
            

            【讨论】:

              【解决方案13】:
              import os,requests
              def download(url):
                  get_response = requests.get(url,stream=True)
                  file_name  = url.split("/")[-1]
                  with open(file_name, 'wb') as f:
                      for chunk in get_response.iter_content(chunk_size=1024):
                          if chunk: # filter out keep-alive new chunks
                              f.write(chunk)
              
              
              download("https://example.com/example.jpg")
              

              【讨论】:

              • 谢谢,另外,将with open(file_name,... 替换为with open('thisname'...),因为它可能会引发错误
              【解决方案14】:

              在 python3 中,您可以使用 urllib3 和 shutil libraires。 使用 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

              【讨论】:

                【解决方案15】:

                简单但Python 2 &amp; Python 3兼容的方式带有six库:

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

                【讨论】:

                • 这是实现 2+3 兼容性的最佳方式。
                【解决方案16】:

                如果速度对你很重要,我对模块urllibwget 做了一个小的性能测试,关于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 好像是最快的

                【讨论】:

                • 一定有什么可怕的事情发生在幕后,让酒吧的时间增加了这么多。
                【解决方案17】:

                适用于 Python 2/3 的 PabloG 代码的改进版本:

                #!/usr/bin/env python
                # -*- coding: utf-8 -*-
                from __future__ import ( division, absolute_import, print_function, unicode_literals )
                
                import sys, os, tempfile, logging
                
                if sys.version_info >= (3,):
                    import urllib.request as urllib2
                    import urllib.parse as urlparse
                else:
                    import urllib2
                    import urlparse
                
                def download_file(url, dest=None):
                    """ 
                    Download and save a file specified by url to dest directory,
                    """
                    u = urllib2.urlopen(url)
                
                    scheme, netloc, path, query, fragment = urlparse.urlsplit(url)
                    filename = os.path.basename(path)
                    if not filename:
                        filename = 'downloaded.file'
                    if dest:
                        filename = os.path.join(dest, filename)
                
                    with open(filename, 'wb') as f:
                        meta = u.info()
                        meta_func = meta.getheaders if hasattr(meta, 'getheaders') else meta.get_all
                        meta_length = meta_func("Content-Length")
                        file_size = None
                        if meta_length:
                            file_size = int(meta_length[0])
                        print("Downloading: {0} Bytes: {1}".format(url, file_size))
                
                        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 = "{0:16}".format(file_size_dl)
                            if file_size:
                                status += "   [{0:6.2f}%]".format(file_size_dl * 100 / file_size)
                            status += chr(13)
                            print(status, end="")
                        print()
                
                    return filename
                
                if __name__ == "__main__":  # Only run if this file is called directly
                    print("Testing with 10MB download")
                    url = "http://download.thinkbroadband.com/10MB.zip"
                    filename = download_file(url)
                    print(filename)
                

                【讨论】:

                • 我会从第一行删除括号,因为它不是太旧的功能。
                【解决方案18】:

                这可能有点晚了,但是我看到了 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 中,它应该是“清晰的”。

                【讨论】:

                • cls 在我的 OS X 或我的 Ubuntu 服务器上没有任何作用。一些澄清可能会很好。
                • 我认为你应该使用clear for linux,或者更好地替换打印行而不是清除整个命令行输出。
                • 这个答案只是复制另一个答案并添加一个对已弃用函数 (os.system()) 的调用,该函数启动一个子进程以使用特定于平台的命令 (cls) 清除屏幕。这怎么会有 any 赞成票?完全没有价值的“答案”恕我直言。
                【解决方案19】:

                我编写了以下代码,适用于普通 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。

                【讨论】:

                • 效果很好,通过 jupyter 运行得到我想要的 :-)
                【解决方案20】:

                以下是python中下载文件最常用的调用:

                1. urllib.urlretrieve ('url_to_file', file_name)

                2. urllib2.urlopen('url_to_file')

                3. requests.get(url)

                4. wget.download('url', file_name)

                注意:urlopenurlretrieve 在下载大文件(大小 > 500 MB)时性能相对较差。 requests.get 将文件存储在内存中,直到下载完成。

                【讨论】:

                  【解决方案21】:
                  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 以二进制模式打开一个文件(并删除任何现有文件),因此您可以使用它保存数据而不仅仅是文本。

                  【讨论】:

                  • 此解决方案的缺点是,整个文件在保存到磁盘之前被加载到内存中,如果在小型系统(如内存有限的路由器)上使用它来处理大文件,请记住这一点.
                  • @tripplet 那么我们该如何解决呢?
                  • 为避免将整个文件读入内存,请尝试将参数传递给file.read,即要读取的字节数。见:gist.github.com/hughdbrown/c145b8385a2afa6570e2
                  • @hughdbrown 我发现您的脚本很有用,但有一个问题:我可以使用该文件进行后期处理吗?假设我下载了一个我想用 OpenCV 处理的 jpg 文件,我可以使用“数据”变量继续工作吗?还是我必须从下载的文件中重新读取?
                  • 改用shutil.copyfileobj(mp3file, output)
                  【解决方案22】:

                  如果你安装了 wget,你可以使用 parallel_sync。

                  pip install parallel_sync

                  from parallel_sync import wget
                  urls = ['http://something.png', 'http://somthing.tar.gz', 'http://somthing.zip']
                  wget.download('/tmp', urls)
                  # or a single file:
                  wget.download('/tmp', urls[0], filenames='x.zip', extract=True)
                  

                  文档: https://pythonhosted.org/parallel_sync/pages/examples.html

                  这很强大。它可以并行下载文件,失败重试,甚至可以下载远程机器上的文件。

                  【讨论】:

                  • 请注意,这仅适用于 Linux
                  【解决方案23】:

                  您也可以通过 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)
                  

                  【讨论】:

                    【解决方案24】:

                    源码可以是:

                    import urllib
                    sock = urllib.urlopen("http://diveintopython.org/")
                    htmlSource = sock.read()                            
                    sock.close()                                        
                    print htmlSource  
                    

                    【讨论】:

                      【解决方案25】:

                      为此目的,用纯 Python 编写了 wget 库。从 2.0 版开始,它在 urlretrievethese features 上得到提升。

                      【讨论】:

                      • 没有使用自定义文件名保存的选项?
                      • @Alex 在 2.1 版本中添加了 -o FILENAME 选项
                      • 我在Cygwin下使用这个模块时没有出现进度条。
                      • 您应该从 -o 更改为 -O 以避免混淆,就像在 GNU wget 中一样。或者至少两个选项都应该有效。
                      • @eric 我不确定我是否想让wget.py 就地替代真正的wget-o 的行为已经有所不同 - 它以这种方式与 curl 兼容。文档中的注释是否有助于解决问题?或者它是命令行兼容的实用程序的基本功能?
                      【解决方案26】:

                      我同意 Corey 的观点,urllib2 比 urllib 更完整,如果你想做更复杂的事情,它应该是使用的模块,但是为了让答案更完整,如果你只想要 urllib 是一个更简单的模块基础知识:

                      import urllib
                      response = urllib.urlopen('http://www.example.com/sound.mp3')
                      mp3 = response.read()
                      

                      会正常工作。或者,如果您不想处理“响应”对象,您可以直接调用 read()

                      import urllib
                      mp3 = urllib.urlopen('http://www.example.com/sound.mp3').read()
                      

                      【讨论】:

                        猜你喜欢
                        • 2021-01-29
                        • 1970-01-01
                        • 1970-01-01
                        • 2011-09-26
                        • 2010-12-25
                        • 2015-07-07
                        相关资源
                        最近更新 更多