I am really new to python and trying to download a .zip file from the URL and extract it on my system using the following code:
# importing necessary modules
import requests, zipfile
from io import BytesIO
print('Downloading started')
#Defining the zip file URL
url = 'https://enfxfr.dol.gov/data_catalog/MSHA/msha_accident_20230422.csv.zip'
# Split URL to get the file name
filename = url.split('/')[-1]
# Downloading the file by sending the request to the URL
req = requests.get(url)
print('Downloading Completed')
# extracting the zip file contents
zipfile= zipfile.ZipFile(BytesIO(req.content))
zipfile.extractall('C:/Users/ssawant/OneDrive - Iron Senergy/Desktop/MSHA converted data/msha_accident')
but getting this error instead:
Traceback (most recent call last):
File "c:\Users\ssawant\OneDrive - Iron Senergy\Desktop\MSHA converted data\test_2.py", line 17, in <module>
zipfile= zipfile.ZipFile(BytesIO(req.content))
File "C:\Users\ssawant\AppData\Local\Programs\Python\Python39\lib\zipfile.py", line 1266, in __init__
self._RealGetContents()
File "C:\Users\ssawant\AppData\Local\Programs\Python\Python39\lib\zipfile.py", line 1333, in _RealGetContents
raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file
I was expecting CSV file extracted from the .zip file.