Get attached PDF file from HTTP request

Viewed 277

I would like to download a file like this: https://www.bbs.unibo.it/conferma/?var=FormScaricaBrochure&brochureid=61305 with Python.

The problem is that is not directly a link to the file, but I only get the file id with query string.

I tried this code:

    import requests

    remote_url = "https://www.bbs.unibo.it/conferma/"

    r = requests.get(remote_url, params = {"var":"FormScaricaBrochure", "brochureid": 61305})

But only the HTML is returned. How can I get the attached pdf?

1 Answers

You can use this example how to download the file using only brochureid:

import requests

url = "https://www.bbs.unibo.it/wp-content/themes/bbs/brochure-download.php?post_id={brochureid}&presentazione=true"
brochureid = 61305

with open("file.pdf", "wb") as f_out:
    f_out.write(requests.get(url.format(brochureid=brochureid)).content)

Downloads the PDF to file.pdf (screenshot):

enter image description here

Related