3

I am newcomer in Python and found a code that downloads and saves data as demofile.csv

   import requests

    url = "https://example.com/demofile"
    r = requests.get(url)

    filename = url.split('/')[-1]

    with open(filename+".csv", "wb") as code:
        code.write(r.content)

Now, I don't want to explicitly specify any name. I just want that URL is opened through Python script and the file gets downloaded with its default name and type(the one that comes when we manually download the file).

Also, this file should be saved in some other directory instead of the folder in which python code is saved.

Kindly help in this.

Aditya
  • 615
  • 3
  • 12
  • 26
  • Have you tried: http://stackoverflow.com/questions/16694907/how-to-download-large-file-in-python-with-requests-py ? – Nf4r Aug 23 '16 at 13:55
  • Yes Nf4r. I tried it. But that code is giving file's name explicitly. I want to download file with its default name. How to do that ?? – Aditya Aug 23 '16 at 13:58

1 Answers1

5

You need to look into 'Content-Disposition' header, see the solution by kender.

How to download a file using python in a 'smarter' way?

Posting his solution modified with a capability to specify an output folder:

from os.path import basename
import os
from urlparse import urlsplit
import urllib2

def url2name(url):
    return basename(urlsplit(url)[2])

def download(url, out_path):
    localName = url2name(url)
    req = urllib2.Request(url)
    r = urllib2.urlopen(req)
    if r.info().has_key('Content-Disposition'):
        # If the response has Content-Disposition, we take file name from it
        localName = r.info()['Content-Disposition'].split('filename=')[1]
        if localName[0] == '"' or localName[0] == "'":
            localName = localName[1:-1]
    elif r.url != url: 
        # if we were redirected, the real file name we take from the final URL
        localName = url2name(r.url)

    localName = os.path.join(out_path, localName)
    f = open(localName, 'wb')
    f.write(r.read())
    f.close()

download("https://example.com/demofile", '/home/username/tmp')
Community
  • 1
  • 1
Maksym
  • 1,430
  • 1
  • 11
  • 13
  • Thanks a lot. It works :) – Aditya Aug 23 '16 at 14:31
  • If our URL has multiple links to download different files, how can we select a particular link. NOTE : Clicking on our desired link downloads the file. – Aditya Aug 23 '16 at 14:41
  • 1
    If I understand correctly, you are referring to a case where a web page has multiple links and you want to download all files associated with those links. For this, you will need to parse your page to identify all the links, and then use the procedure above to go and download these links. There are many resources on how to parse a page with python, for example see here: http://stackoverflow.com/questions/3075550/how-can-i-get-href-links-from-html-code – Maksym Aug 23 '16 at 17:24
  • 1
    You can also search for tools that crawl or mirror a web page- just google for 'python parse links from html and download files': http://stackoverflow.com/questions/1825438/download-html-page-and-its-content https://pypi.python.org/pypi/spider.py/0.5 http://www.diveintopython.net/html_processing/extracting_data.html – Maksym Aug 23 '16 at 17:27