I have a link to a PDF file that I would like to download. I tried the following:
import requests
class Scraper:
def __init__(self):
"""Init the class"""
@staticmethod
def download(full_url):
"""Download full url pdf"""
with requests.Session() as req:
# Init
r = req.get(full_url, allow_redirects=True)
localname = 'test.pdf'
# Download
if r.status_code == 200: #and r.headers['Content-Type'] == "application/pdf;charset=UTF-8":
with open(f"{localname}", 'wb') as f:
f.write(r.content)
else:
pass
However, after downloading, when I try to open it on my computer I receive the message:
"Could not open [FILENAME].pdf because it is either not a supported file type or because the file has been damaged (...)"
- What is the reason for this? Is it because the first time you visit this page you get redirected and you need to select some preferences?
- How can we resolve this?