0

I have a link that when put into the web browser, a pop-up question then asks whether to open or save the file. I have tried a few solutions, but nothing worked as expected.

I have tried implementing the solutions from this post here: Download Returned Zip file from URL. But without having any results. I managed to create the zip file, but what I have problems with, is the transition from the URL to writing to the zip file.

import requests, zipfile

path = "A:/"

url = "http://"+"www.sec.gov/dera/data/Public-EDGAR-log-file-data/2003 /Qtr4/log20031231.zip"
target_path = path + 'log20031231.zip'


results = requests.get(url)
zip = zipfile.ZipFile(path + 'log20031231_2.zip', "w")
insert = zipfile.ZipFile(results.content)
zip.write(results, compress_type=zipfile.ZIP_DEFLATED)
zip.close()

The code creates the zip , but nothing is written to the zip file. The output in the console is the following:

Traceback (most recent call last):
  File "A:/python project/testing/testing 3.py", line 17, in <module>
    insert = zipfile.ZipFile(results.content)
  File "C:\Users\Adrian\AppData\Local\Programs\Python\Python37\lib\zipfile.py", line 1222, in __init__
    self._RealGetContents()
  File "C:\Users\Adrian\AppData\Local\Programs\Python\Python37\lib\zipfile.py", line 1285, in _RealGetContents
    endrec = _EndRecData(fp)
  File "C:\Users\Adrian\AppData\Local\Programs\Python\Python37\lib\zipfile.py", line 259, in _EndRecData
    fpin.seek(0, 2)
AttributeError: 'bytes' object has no attribute 'seek'
Adrian
  • 715
  • 5
  • 21

1 Answers1

0

I have found a solution to the problem. This does not even need the zip file module. It downloads the contents and then it puts them into the archive.

import requests

url = "http://"+"www.sec.gov/dera/data/Public-EDGAR-log-file-data/2003/Qtr4/log20031231.zip"
open_url = requests.get(url)

with open('log20031231_2.zip', "wb") as code:
    code.write(open_url.content)
Adrian
  • 715
  • 5
  • 21