I\'m using the Python requests lib to get a PDF file from the web. This works fine, but I now also want the original filename. If I go to a PDF file in Firefox and click
Building on some of the other answers, here's how I do it. If there isn't a Content-Disposition
header, I parse it from the download URL:
import re
import requests
from requests.exceptions import RequestException
url = 'http://www.example.com/downloads/sample.pdf'
try:
with requests.get(url) as r:
fname = ''
if "Content-Disposition" in r.headers.keys():
fname = re.findall("filename=(.+)", r.headers["Content-Disposition"])[0]
else:
fname = url.split("/")[-1]
print(fname)
except RequestException as e:
print(e)
There are arguably better ways of parsing the URL string, but for simplicity I didn't want to involve any more libraries.