4

I work on a project and I want to download a csv file from a url. I did some research on the site but none of the solutions presented worked for me.

The url offers you directly to download or open the file of the blow I do not know how to say a python to save the file (it would be nice if I could also rename it)

But when I open the url with this code nothing happens.

import urllib
url='https://data.toulouse-metropole.fr/api/records/1.0/download/?dataset=dechets-menagers-et-assimiles-collectes'

testfile = urllib.request.urlopen(url)

Any ideas?

blabla
  • 402
  • 2
  • 10
  • 17
  • 1
    Have you taken a look at this question: http://stackoverflow.com/questions/19602931/basic-http-file-downloading-and-saving-to-disk-in-python – roach Jan 02 '17 at 23:16
  • Yes! the answers is : testfile = urllib.URLopener() testfile.retrieve("http://randomsite.com/file.gz", "file.gz") but i got errors: 'HTTPResponse' object has no attribute 'retrieve' – blabla Jan 02 '17 at 23:21

2 Answers2

14

Try this. Change "folder" to a folder on your machine

import os
import requests

url='https://data.toulouse-metropole.fr/api/records/1.0/download/?dataset=dechets-menagers-et-assimiles-collectes'
response = requests.get(url)
with open(os.path.join("folder", "file"), 'wb') as f:
    f.write(response.content)
somesingsomsing
  • 3,182
  • 4
  • 29
  • 46
1

You can adapt an example from the docs

import urllib.request
url='https://data.toulouse-metropole.fr/api/records/1.0/download/?dataset=dechets-menagers-et-assimiles-collectes'

with urllib.request.urlopen(url) as testfile, open('dataset.csv', 'w') as f:
    f.write(testfile.read().decode())
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61