Search on blog:

Python: How to download PDF on US Department of Commerce using requests.

There is button "View attachment file" on page

which can download PDF.

Problem is that this button doesn't have href with direct link to PDF but it uses JavaScript to get it.

First idea is to use [Selenium]( to download it but using DevTools (tab: Network, option: Presist Logs) in Firefox you can see that this button first send request to url

and it gets JSON data with "downloadURL" which gives url to PDF.

So using requests with first url we can get JSON data with url to PDF and then we can use again requests to download PDF.

import requests
import webbrowser

number = 800

url = f'{number}?handler=DownloadDM&ID={number}'

r = requests.get(url)
data = r.json()
print('url:', data["downloadURL"])

filename = f'output-{number}.pdf'

r = requests.get(data["downloadURL"])
with open(filename, 'wb') as fh:

# open PDF in default program

Using number different then 800 you can download PDF from other pages.

If you like it
Buy a Coffee