【问题标题】:Python : extract files from zipfile from web without downloading and saving it firstPython:从 web 的 zipfile 中提取文件,无需先下载并保存
【发布时间】:2020-10-09 14:18:10
【问题描述】:
import re
import requests
import zipfile
import werkzeug
werkzeug.cached_property = werkzeug.utils.cached_property

from robobrowser import RoboBrowser

br = RoboBrowser(history=True)
br.open("loginurl")
forms = br.get_forms()

form=forms[0]

form['username'] = 'myname'
form['password'] = 'mypass'
br.submit_form(form)

url = "ulrlocationofzipfiledownload"
request = br.session.get(url, stream=True)

with open ('data.zip', 'wb') as f:
           f.write(request.content)

到目前为止,这可以工作,它会下载一个 zip 文件(到我的脚本所在的桌面)

我希望它从压缩文件中提取特定内容到特定位置,而不在我的桌面上创建压缩文件

我尝试了这段代码的一些变体:

with zipfile.ZipFile('files.zip','r') as f:
      myzipfile.extractall('files')

但我无法让它工作。

是否可以从 Web 获取 zipfile 并将其某些特定内容提取到 PC 上的特定位置,而无需先保存下载的 zipfile(也许只在 RAM 中保持 zipfile“打开”?)

【问题讨论】:

  • 您可以将 zip 文件下载到某个临时位置,并在完成后将其删除。您当然不必将其保存到桌面。除非“桌面”是指“计算机”。

标签: python zipfile robobrowser


【解决方案1】:

您可以避免实际保存文件,但使用requests

import requests, zipfile, io

url = 'url-to-zipfile'

response = requests.get(url, stream=True)
zipfile = zipfile.ZipFile(io.BytesIO(response.content))

zipfile.extractall()

我不确定RoboBrowser 是否能够做到这一点。

【讨论】:

    【解决方案2】:

    我做到了:)

    import requests
    import zipfile
    import werkzeug
    import io
    import subprocess
    
    werkzeug.cached_property = werkzeug.utils.cached_property
    
    from robobrowser import RoboBrowser
    
    br = RoboBrowser(history=True)
    br.open("loginurlhere")
    forms = br.get_forms()
    
    form=forms[0]   #when there are multiple forms in website, which form to select? (0=1st)
    
    form['username'] = 'myusername'
    form['password'] = 'mypass'
    br.submit_form(form)
    
    url1 = "DirectUrlof1stFileToDownload"
    
    url2 = "DirectUrlof2ndFileToDownload"
    
    # D:\Games\..\...  represent an example of location where to extract the file(s)
    response1 = br.session.get(url1, stream=True)
    zipfile1 = zipfile.ZipFile(io.BytesIO(response1.content))
    zipfile1.extract('nameOftheFileInsideZipToExtract','D:\Games\..\...')
    
    response2 = br.session.get(url2, stream=True)
    zipfile2 = zipfile.ZipFile(io.BytesIO(response2.content))
    zipfile2.extract('nameOftheFileInsideZipToExtract','D:\Games\..\...')
    zipfile2.extract('nameOftheFileInsideZipToExtract','D:\Games\..\...')
    zipfile2.extract('nameOftheFileInsideZipToExtract','D:\Games\..\...')
    
    
    #For executing a specific file (I use it to execute an extracted file)
    subprocess.call([r'D:\Games\..\...'])
    
    
    
    ''' For saving a zip file
    request = br.session.get(url1, stream=True)
    with open ('data.zip', 'wb') as f:
               f.write(request.content)
    '''
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-23
      • 2011-06-22
      • 2019-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-18
      相关资源
      最近更新 更多