<small id='JJKYy'></small><noframes id='JJKYy'>

  • <i id='JJKYy'><tr id='JJKYy'><dt id='JJKYy'><q id='JJKYy'><span id='JJKYy'><b id='JJKYy'><form id='JJKYy'><ins id='JJKYy'></ins><ul id='JJKYy'></ul><sub id='JJKYy'></sub></form><legend id='JJKYy'></legend><bdo id='JJKYy'><pre id='JJKYy'><center id='JJKYy'></center></pre></bdo></b><th id='JJKYy'></th></span></q></dt></tr></i><div id='JJKYy'><tfoot id='JJKYy'></tfoot><dl id='JJKYy'><fieldset id='JJKYy'></fieldset></dl></div>

    • <bdo id='JJKYy'></bdo><ul id='JJKYy'></ul>

    <legend id='JJKYy'><style id='JJKYy'><dir id='JJKYy'><q id='JJKYy'></q></dir></style></legend>

      <tfoot id='JJKYy'></tfoot>

        在 Python 3 中从 Web 下载文件

        时间:2024-04-21

          <tfoot id='gXTD9'></tfoot>
          <i id='gXTD9'><tr id='gXTD9'><dt id='gXTD9'><q id='gXTD9'><span id='gXTD9'><b id='gXTD9'><form id='gXTD9'><ins id='gXTD9'></ins><ul id='gXTD9'></ul><sub id='gXTD9'></sub></form><legend id='gXTD9'></legend><bdo id='gXTD9'><pre id='gXTD9'><center id='gXTD9'></center></pre></bdo></b><th id='gXTD9'></th></span></q></dt></tr></i><div id='gXTD9'><tfoot id='gXTD9'></tfoot><dl id='gXTD9'><fieldset id='gXTD9'></fieldset></dl></div>
              <bdo id='gXTD9'></bdo><ul id='gXTD9'></ul>

              <small id='gXTD9'></small><noframes id='gXTD9'>

                • <legend id='gXTD9'><style id='gXTD9'><dir id='gXTD9'><q id='gXTD9'></q></dir></style></legend>
                    <tbody id='gXTD9'></tbody>
                • 本文介绍了在 Python 3 中从 Web 下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I am creating a program that will download a .jar (java) file from a web server, by reading the URL that is specified in the .jad file of the same game/application. I'm using Python 3.2.1

                  I've managed to extract the URL of the JAR file from the JAD file (every JAD file contains the URL to the JAR file), but as you may imagine, the extracted value is type() string.

                  Here's the relevant function:

                  def downloadFile(URL=None):
                      import httplib2
                      h = httplib2.Http(".cache")
                      resp, content = h.request(URL, "GET")
                      return content
                  
                  downloadFile(URL_from_file)
                  

                  However I always get an error saying that the type in the function above has to be bytes, and not string. I've tried using the URL.encode('utf-8'), and also bytes(URL,encoding='utf-8'), but I'd always get the same or similar error.

                  So basically my question is how to download a file from a server when the URL is stored in a string type?

                  解决方案

                  If you want to obtain the contents of a web page into a variable, just read the response of urllib.request.urlopen:

                  import urllib.request
                  ...
                  url = 'http://example.com/'
                  response = urllib.request.urlopen(url)
                  data = response.read()      # a `bytes` object
                  text = data.decode('utf-8') # a `str`; this step can't be used if data is binary
                  


                  The easiest way to download and save a file is to use the urllib.request.urlretrieve function:

                  import urllib.request
                  ...
                  # Download the file from `url` and save it locally under `file_name`:
                  urllib.request.urlretrieve(url, file_name)
                  

                  import urllib.request
                  ...
                  # Download the file from `url`, save it in a temporary directory and get the
                  # path to it (e.g. '/tmp/tmpb48zma.txt') in the `file_name` variable:
                  file_name, headers = urllib.request.urlretrieve(url)
                  

                  But keep in mind that urlretrieve is considered legacy and might become deprecated (not sure why, though).

                  So the most correct way to do this would be to use the urllib.request.urlopen function to return a file-like object that represents an HTTP response and copy it to a real file using shutil.copyfileobj.

                  import urllib.request
                  import shutil
                  ...
                  # Download the file from `url` and save it locally under `file_name`:
                  with urllib.request.urlopen(url) as response, open(file_name, 'wb') as out_file:
                      shutil.copyfileobj(response, out_file)
                  

                  If this seems too complicated, you may want to go simpler and store the whole download in a bytes object and then write it to a file. But this works well only for small files.

                  import urllib.request
                  ...
                  # Download the file from `url` and save it locally under `file_name`:
                  with urllib.request.urlopen(url) as response, open(file_name, 'wb') as out_file:
                      data = response.read() # a `bytes` object
                      out_file.write(data)
                  


                  It is possible to extract .gz (and maybe other formats) compressed data on the fly, but such an operation probably requires the HTTP server to support random access to the file.

                  import urllib.request
                  import gzip
                  ...
                  # Read the first 64 bytes of the file inside the .gz archive located at `url`
                  url = 'http://example.com/something.gz'
                  with urllib.request.urlopen(url) as response:
                      with gzip.GzipFile(fileobj=response) as uncompressed:
                          file_header = uncompressed.read(64) # a `bytes` object
                          # Or do anything shown above using `uncompressed` instead of `response`.
                  

                  这篇关于在 Python 3 中从 Web 下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为什么 Python 3.6.1 抛出 AttributeError:模块 'enum' 没有属性 &# 下一篇:“用于线路输入..."导致 UnicodeDecodeError: 'utf-8' codec

                  相关文章

                      <legend id='tB3ST'><style id='tB3ST'><dir id='tB3ST'><q id='tB3ST'></q></dir></style></legend>
                    1. <tfoot id='tB3ST'></tfoot>
                    2. <small id='tB3ST'></small><noframes id='tB3ST'>

                      <i id='tB3ST'><tr id='tB3ST'><dt id='tB3ST'><q id='tB3ST'><span id='tB3ST'><b id='tB3ST'><form id='tB3ST'><ins id='tB3ST'></ins><ul id='tB3ST'></ul><sub id='tB3ST'></sub></form><legend id='tB3ST'></legend><bdo id='tB3ST'><pre id='tB3ST'><center id='tB3ST'></center></pre></bdo></b><th id='tB3ST'></th></span></q></dt></tr></i><div id='tB3ST'><tfoot id='tB3ST'></tfoot><dl id='tB3ST'><fieldset id='tB3ST'></fieldset></dl></div>
                        <bdo id='tB3ST'></bdo><ul id='tB3ST'></ul>