<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 下载文件

        Download file from web in Python 3(在 Python 3 中从 Web 下载文件)

          <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 下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)

                      <legend id='tB3ST'><style id='tB3ST'><dir id='tB3ST'><q id='tB3ST'></q></dir></style></legend>
                    1. <tfoot id='tB3ST'></tfoot>
                        <tbody id='tB3ST'></tbody>
                      • <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>