requests module produces some corrupted file when I attempt to download a pdf file from a link

Viewed 52

I'm trying to download a pdf file from a link using requests module but the script produces some corrupted file. When the download appears to be complete and I try to open it manually, I get the error 'Failed to load PDF content'.

However, when I use another link from the same site, the script can download it smoothly.

I'm using this script to download the file:

import requests

link = 'https://colwood.civicweb.net/filepro/document/197721/RZ-22-012.pdf?handle=99B7BEBB4DFB4EB987761DA873019A6D'

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
}
res = requests.get(link,headers=headers)
with open("demo.pdf","wb") as f:
    for data in res.iter_content(1024):
        if data:
            f.write(data)

How can I download the pdf file from the link within the script using requests module?

0 Answers
Related