-2

there is website with url

https://www.fda.gov/drugs/drug-approvals-and-databases/drugsfda-data-files

and there is one downloadable file

Drugs@FDA Download File (ZIP - 3.2MB) as Hyperlink in the content of the site.

I have tried the code as below

import urllib.request
import gzip
url = 'https://www.fda.gov/media/89850/download'

with urllib.request.urlopen(url) as response:
    with gzip.GzipFile(fileobj=response) as uncompressed:
        file_header = uncompressed.read()

But i am getting error of : Not a Zipped file

chandu
  • 23
  • 8
  • 6
    Possible duplicate of [Download file from web in Python 3](https://stackoverflow.com/questions/7243750/download-file-from-web-in-python-3) – Chris Doyle Nov 25 '19 at 08:10
  • How to get the url of zip file. – chandu Nov 25 '19 at 08:13
  • 1
    In the webpage how do you download the zip file ? you click the link right? so just copy that link....https://www.fda.gov/media/89850/download – Chris Doyle Nov 25 '19 at 08:15
  • Hi @ChrisDoyle i have tried the following code , but i am getting error, can you please look into it – chandu Nov 25 '19 at 11:51

1 Answers1

0

you can use the python requests library to get the data from the url then write the contents to a file.

import requests

with open('my_zip_file.zip', 'wb') as my_zip_file:
    data = requests.get('https://www.fda.gov/media/89850/download')
    my_zip_file.write(data.content)

This will create a file in the same directory. you can of course name your file anything.

Chris Doyle
  • 10,703
  • 2
  • 23
  • 42