When downloading the PDF from the specified URL using the requests library in Python, the resulting file is distorted. There are lines across the pages and some blank pages. However, downloading the same PDF directly from Chrome results in a perfect file.
import requests
headers = {
'Accept': 'application/pdf',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36',
}
params = {
'doc': '3150239',
}
response = requests.get('https://adxservices.adx.ae/cdn/contentdownload.aspx', params=params, cookies=cookies, headers=headers)
# Check if the response is a PDF
if response.headers.get('Content-Type') == 'application/pdf':
# Save the content as a PDF file
with open('document.pdf', 'wb') as file:
file.write(response.content)
print("PDF downloaded successfully.")
else:
print(f"Unexpected content type: {response.headers.get('Content-Type')}")
with open('response.html', 'wb') as file:
file.write(response.content)
print("Response saved as HTML for inspection.")
type here
New contributor
Will James is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.