1
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)

As of now, this works and it downloads a zipfile (to my desktop where the script is located)

I want it to extract specific contents from the zipfile to a specific location without creating the zipfile on my desktop

I tried some variations of this code:

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

but I couldn't get it to work.

Is it possible to get zipfile from the web and extract some of its specific contents to a specific locations on PC, all without saving the downloaded zipfile first (maybe only keeping the zipfile "open" in RAM ?)

Dharman
  • 30,962
  • 25
  • 85
  • 135
mato200
  • 75
  • 6
  • 1
    You can download the zip file to some temporary location and delete it when you're done with it. You certainly don't have to save it to your desktop. Unless by "desktop" you mean "computer". – khelwood Jun 19 '20 at 10:37

2 Answers2

3

You can avoid to actually save the file, but using requests

import requests, zipfile, io

url = 'url-to-zipfile'

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

zipfile.extractall()

I'm not sure RoboBrowser is able to do that.

Arount
  • 9,853
  • 1
  • 30
  • 43
2

I made it :)

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)
'''
mato200
  • 75
  • 6