0

I want to download a file using requests since its the best option from my research so far but I can't find out how to download the given url link without giving a file name.

import requests

url = 'https://www.python.org/static/img/python-logo.png'
fileName = 'D:\Python\dwnldPythonLogo.png'
req = requests.get(url)
file = open(fileName, 'wb')
for chunk in req.iter_content(100000):
    file.write(chunk)
file.close()

The code above shows how to download it with a file name, is there a way to do it without a file name?

For example like this:


import requests

url = 'https://www.python.org/static/img/python-logo.png'
location = 'D:\Python\'
req = requests.get(url)
file = open(location, 'wb')
for chunk in req.iter_content(100000):
    file.write(chunk)
file.close()
aoeu
  • 153
  • 3
  • 15
  • Not clear what you expect... How do you download something without giving a name where to save to? – OneCricketeer Apr 05 '19 at 16:07
  • Okay, so `open('wb')` isn't valid syntax. You need a filename there – OneCricketeer Apr 05 '19 at 16:10
  • @cricket_007 No, like saying where to download it without also giving it a file name. Like how you can download files off the browser without needing to name the file and just use the name it was already given. – aoeu Apr 05 '19 at 16:10
  • you'd want something like `file = open(url.split('/')[-1], 'wb'):` which will download the file with name as what is after the last slash in the url into your current directory – jeremycg Apr 05 '19 at 16:12
  • Possible duplicate of [Download large file in python with requests](https://stackoverflow.com/questions/16694907/download-large-file-in-python-with-requests). See it splits the url – OneCricketeer Apr 05 '19 at 16:12
  • @cricket_007 The problem with that is the url of the file always changes and isn't consistent. – aoeu Apr 05 '19 at 16:14
  • Not sure what you mean but if you want to choose the download location during runtime you can use the input() function. – lordy Apr 05 '19 at 16:16
  • @lordy What I mean is I want to download the file with the file name as the name it has on the internet and not specify what it will be when I download it. – aoeu Apr 05 '19 at 16:22
  • Why does it matter if it changes? `http://url.com/a.txt` will download `a.txt` and `http://something.org/b.txt` will get `b.txt`... Did I miss something else? – OneCricketeer Apr 05 '19 at 16:27
  • @cricket_007 The url looks like this: https://scontent-ort2-1.cdninstagram.com/vp/322b5395ee9ef27a3c6a588147e2a1f3/5CAA1824/t51.12442-15/e15/p640x640/38291305_273562119909817_4197279119550971904_n.jpg?_nc_ht=scontent-ort2-1.cdninstagram.com – aoeu Apr 05 '19 at 16:30

1 Answers1

4
import requests
import re

def get_filename_from_cd(cd):
    """
    Get filename from content-disposition
    """
    if not cd:
        return None
    fname = re.findall('filename=(.+)', cd)
    if len(fname) == 0:
        return None
    return fname[0]


url = 'http://google.com/favicon.ico'
r = requests.get(url, allow_redirects=True)
filename = get_filename_from_cd(r.headers.get('content-disposition'))
open(filename, 'wb').write(r.content)

I think this fixed my problem since it gets the file name from the headers rather than the url.

Source

aoeu
  • 153
  • 3
  • 15
  • 1
    Does not work. Throws TypeError: expected str, bytes or os.PathLike object, not NoneType (for last line) – brikas Jul 08 '21 at 07:55