【问题标题】:How to download files directly into an archive?如何将文件直接下载到存档中?
【发布时间】:2021-10-01 13:21:32
【问题描述】:

我想下载多个文件并将它们保存在一个 zip 文件中。

到目前为止,这是我的代码:

import zipfile
import requests
import os

pics = ['url/1.jpg', 'url/2.jpg', 'url/3.jpg']
dir = '/directory' 

with zipfile.ZipFile(dir + '/test.zip', 'w') as my_zip:
    for x in range(len(pics)):
        fn = dir + '/' + pics[x].split('/')[-1]
        
        r = requests.get(pics[x], allow_redirects=True)
        open(fn, 'wb').write(r.content)
    
        my_zip.write(fn, str(x+1) + os.path.splitext(fn)[1])
        
        os.remove(fn)

有没有更智能/更清洁/更短的方法来做到这一点。 下载 img,然后将其放入 zip 中,然后删除它似乎不必要地复杂。

【问题讨论】:

    标签: python download request zip python-zipfile


    【解决方案1】:

    我认为在这种情况下你应该给你一个临时文件。

    这应该可行:

    import requests
    import zipfile
    import tempfile
    
    pics = ['http://www.princeton.edu/~dancexp/Images/1-XPAlifun.jpg',
            'http://www.princeton.edu/~dancexp/Images/1-ExposeJess.jpg',
            'http://www.princeton.edu/~dancexp/Images/JessHsurevisedlores.jpg']
    
    picdirname = "mypics"
    
    with zipfile.ZipFile("test.zip", "w") as my_zip:
                         
        for pic in pics:
            response = requests.get(pic)
            if response.status_code == 200:
                tmpf = tempfile.TemporaryFile()
                tmpf.write(response.content)
                tmpf.seek(0)
                my_zip.writestr(pic.split('/')[-1], tmpf.read())
                tmpf.close()
    

    【讨论】:

      【解决方案2】:

      我认为速度也很重要。试试这段代码

      import requests, zipfile, os
      from multiprocessing.dummy import Pool
      directory = 'pics/'
      def Save(url):
          filename = directory+url.split('/')[-1]
          try:
              try:
                  res = requests.get(url)
                  open(filename, 'wb').write(res.content)
              except:
                  return {'filename': filename, 'status': 'cant crate file.'}
          except:
              return {'filename': filename, 'status': 'error'}
          else:
              return {'filename': filename, 'status': 'ok'}
      
      
      urls = ['https://i.stack.imgur.com/kBVja.jpg',
      'https://lh5.googleusercontent.com/-TlrV5ArUF6s/AAAAAAAAAAI/AAAAAAAAAC0/d605oPHpYgc/photo.jpg',
      'https://i.stack.imgur.com/ipDCR.png',
      'https://i.stack.imgur.com/Zpq5l.jpg']
      
      pool_ = Pool(16) #It depends on your processor and the Internet to increase or decrease it
      result = pool_.map(Save, urls)
      
      
      with zipfile.ZipFile('test.zip', 'w') as zip_file:
          for x in result:
              if x['status'] == 'ok':
                  zip_file.write(x['filename'])
                  os.remove(x['filename'])
                  print(x['filename'], " + Added to zip file")
              else:
                  print(x)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-10-16
        • 2011-08-03
        • 2011-10-31
        • 2019-12-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多