How to Download Images and PDF files with Python 3 Requests

How to Download Images and PDF files with Python 3 Requests

import re import requests urls = [‘https://www.example.com/image-1.png’,’https://www.example.com/file.pdf’] for i in urls: r = requests.get(i) d = r.headers[‘content-disposition’] fname = re.findall(“filename=(.+)”, d)[0] # send a HTTP request to the server and save # the HTTP response in a response object called r with open(fname,’wb’) as f: f.write(r.content) print(i+”|”+str(fname))