Home > Net >  Download file from website directly into Linux directory - Python
Download file from website directly into Linux directory - Python

Time:07-22

If I manually click on button, the browser starts downloading a CSV file (2GB) onto my computer. But I want to automate this.

This is the link to download:

https://data.cityofnewyork.us/api/views/bnx9-e6tj/rows.csv?accessType=DOWNLOAD

Issue; when I use either (requests or pandas) libraries it just hangs. I have no idea if it is being downloaded or not.

My goal is to:

  1. Know if the file is being downloaded and
  2. Have the CSV downloaded to a specified directory ie. ~/mydirectory

Can someone provide the code to do this?

CodePudding user response:

Try this...

import requests


URL = "https://data.cityofnewyork.us/api/views/bnx9-e6tj/rows.csv?accessType=DOWNLOAD"

response = requests.get(URL)
print('Download Complete')

open("/mydirectory/downloaded_file.csv", "wb").write(response.content)

Or you could do it this way and have a progress bar ...

import wget

wget.download('https://data.cityofnewyork.us/api/views/bnx9-e6tj/rows.csv?accessType=DOWNLOAD')

The output will look like this:

11% [........                                     ] 73728 / 633847
  • Related