You should use response.content in this case:

with open('/tmp/metadata.pdf', 'wb') as f:
    f.write(response.content)

From the document:

You can also access the response body as bytes, for non-text requests:

>>> r.content
b'[{"repository":{"open_issues":0,"url":"https://github.com/...

So that means: response.text return the output as a string object, use it when you're downloading a text file. Such as HTML file, etc.

And response.content return the output as bytes object, use it when you're downloading a binary file. Such as PDF file, audio file, image, etc.


You can also use response.raw instead. However, use it when the file which you're about to download is large. Below is a basic example which you can also find in the document:

import requests

url = 'http://www.hrecos.org//images/Data/forweb/HRTVBSH.Metadata.pdf'
r = requests.get(url, stream=True)

with open('/tmp/metadata.pdf', 'wb') as fd:
    for chunk in r.iter_content(chunk_size):
        fd.write(chunk)

chunk_size is the chunk size which you want to use. If you set it as 2000, then requests will download that file the first 2000 bytes, write them into the file, and do this again, again and again, unless it finished.

So this can save your RAM. But I'd prefer use response.content instead in this case since your file is small. As you can see use response.raw is complex.


Relates:

  • How to download large file in python with requests.py?

  • How to download image using requests

Answer from Remi Guan on Stack Overflow

You should use response.content in this case:

with open('/tmp/metadata.pdf', 'wb') as f:
    f.write(response.content)

From the document:

You can also access the response body as bytes, for non-text requests:

>>> r.content
b'[{"repository":{"open_issues":0,"url":"https://github.com/...

So that means: response.text return the output as a string object, use it when you're downloading a text file. Such as HTML file, etc.

And response.content return the output as bytes object, use it when you're downloading a binary file. Such as PDF file, audio file, image, etc.


You can also use response.raw instead. However, use it when the file which you're about to download is large. Below is a basic example which you can also find in the document:

import requests

url = 'http://www.hrecos.org//images/Data/forweb/HRTVBSH.Metadata.pdf'
r = requests.get(url, stream=True)

with open('/tmp/metadata.pdf', 'wb') as fd:
    for chunk in r.iter_content(chunk_size):
        fd.write(chunk)

chunk_size is the chunk size which you want to use. If you set it as 2000, then requests will download that file the first 2000 bytes, write them into the file, and do this again, again and again, unless it finished.

So this can save your RAM. But I'd prefer use response.content instead in this case since your file is small. As you can see use response.raw is complex.


Relates:

  • How to download large file in python with requests.py?

  • How to download image using requests

Answer from Remi Guan on Stack Overflow
Top answer
1 of 5
276

You should use response.content in this case:

with open('/tmp/metadata.pdf', 'wb') as f:
    f.write(response.content)

From the document:

You can also access the response body as bytes, for non-text requests:

>>> r.content
b'[{"repository":{"open_issues":0,"url":"https://github.com/...

So that means: response.text return the output as a string object, use it when you're downloading a text file. Such as HTML file, etc.

And response.content return the output as bytes object, use it when you're downloading a binary file. Such as PDF file, audio file, image, etc.


You can also use response.raw instead. However, use it when the file which you're about to download is large. Below is a basic example which you can also find in the document:

import requests

url = 'http://www.hrecos.org//images/Data/forweb/HRTVBSH.Metadata.pdf'
r = requests.get(url, stream=True)

with open('/tmp/metadata.pdf', 'wb') as fd:
    for chunk in r.iter_content(chunk_size):
        fd.write(chunk)

chunk_size is the chunk size which you want to use. If you set it as 2000, then requests will download that file the first 2000 bytes, write them into the file, and do this again, again and again, unless it finished.

So this can save your RAM. But I'd prefer use response.content instead in this case since your file is small. As you can see use response.raw is complex.


Relates:

  • How to download large file in python with requests.py?

  • How to download image using requests

2 of 5
56

In Python 3, I find pathlib is the easiest way to do this. Request's response.content marries up nicely with pathlib's write_bytes.

from pathlib import Path
import requests
filename = Path('metadata.pdf')
url = 'http://www.hrecos.org//images/Data/forweb/HRTVBSH.Metadata.pdf'
response = requests.get(url)
filename.write_bytes(response.content)
🌐
Python
docs.python.org › 3 › download.html
Download — Python 3.14.7 documentation
Download an archive containing all the documentation for this version of Python: We no longer provide pre-built PDFs of the documentation.
People also ask

How can I download a PDF from a URL using Python?
To download a PDF from a URL in Python, you can use IronPDF's built-in Chromium browser to render the URL as a PDF. Use the ChromePdfRenderer class to fetch the content and SaveAs method to save it as a PDF file.
🌐
ironpdf.com
ironpdf.com › ironpdf for python › blog › using ironpdf for python › python download pdf from url
Python Download PDF From URL (Developer Tutorial) | IronPDF for Python
How can I create interactive PDF documents using Python?
IronPDF allows the creation of interactive PDF documents in Python by providing features such as form filling, annotations, and the ability to split and merge PDF files.
🌐
ironpdf.com
ironpdf.com › ironpdf for python › blog › using ironpdf for python › python download pdf from url
Python Download PDF From URL (Developer Tutorial) | IronPDF for Python
What is required to use IronPDF with Python?
To use IronPDF with Python, ensure you have Python and the .NET 6.0 runtime installed. Additionally, install the IronPDF package in your Python environment using pip install ironpdf.
🌐
ironpdf.com
ironpdf.com › ironpdf for python › blog › using ironpdf for python › python download pdf from url
Python Download PDF From URL (Developer Tutorial) | IronPDF for Python
🌐
Techbit
techbit.ca › 2022 › 12 › downloading-pdf-files-using-python
Downloading PDF Files Using Python - Techbit
December 17, 2022 - import requests import re from bs4 import BeautifulSoup # Set user-agent otherwise we get a 403 forbidden error headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; rv:108.0) Gecko/20100101 Firefox/108.0' } url = 'https://tomlehrersongs.com/category/sheet-music/' domain = 'https://tomlehrersongs.com' # Set parameters for regex search ext = '.pdf' pattern = re.compile(ext) # Function to get the links from the site using requests and beautifulSoup def get_links(url,headers): page = requests.get(url,headers=headers) soup = BeautifulSoup(page.text, 'html.parser') links = [str(link.get('href'))
🌐
DEV Community
dev.to › seraph776 › download-pdf-files-using-python-4064
Download PDF Files Using Python - DEV Community
August 1, 2022 - The following program downloads a PDF files from the provided URL. #!/usr/bin/env python3 import os import requests def download_pdf_file(url: str) -> bool: """Download PDF from given URL to local directory. :param url: The url of the PDF file to be downloaded :return: True if PDF file was successfully downloaded, otherwise False.
🌐
IronPDF
ironpdf.com › ironpdf for python › blog › using ironpdf for python › python download pdf from url
Python Download PDF From URL (Developer Tutorial) | IronPDF for Python
April 22, 2026 - Using Python, a PDF file can be easily generated with just a few lines of code using the IronPDF library. IronPDF is a standalone library that does not require any additional dependencies.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › downloading-pdfs-with-python-using-requests-and-beautifulsoup
Downloading PDFs with Python using Requests and BeautifulSoup - GeeksforGeeks
July 23, 2025 - The above program downloads the PDF files from the provided URL with names pdf1, pdf2, pdf3 and so on respectively.
🌐
PyPI
pypi.org › project › pypdf
pypdf · PyPI
pypdf is a free and open-source pure-python PDF library capable of splitting, merging, cropping, and transforming the pages of PDF files. It can also add custom data, viewing options, and passwords to PDF files.
Author: Viren070
🌐
Blog
halvorsen.blog › documents › programming › python › resources › Python Programming.pdf pdf
Python Programming Hans-Petter Halvorsen https://www.halvorsen.blog
Python is a multi-purpose programming language, which can be use for simu- lation, creating web pages, communicate with database systems, etc. ... These resources are a supplement to this textbook. Here you can download the
🌐
GitHub
github.com › CodeWithHarry › The-Ultimate-Python-Course › blob › main › The Ultimate Python Handbook.pdf
The-Ultimate-Python-Course/The Ultimate Python Handbook.pdf at main · CodeWithHarry/The-Ultimate-Python-Course
CodeWithHarry / The-Ultimate-Python-Course Public · Notifications · You must be signed in to change notification settings · Fork 995 · Star 2.1k · main · / Copy path · More file actions · More file actions · History · History · 1.65 MB · main · / Copy pathTop · 1.65 MB · Download raw file ·
Author: CodeWithHarry
🌐
Medium
medium.com › @abdelfatahmennoun4 › how-to-download-pdfs-from-a-webpage-using-python-9758b1e9413f
How to Download PDFs from a Webpage using Python | by Abdelfatah MENNOUN | Medium
May 5, 2023 - Are you tired of manually downloading PDF files from a website? Do you wish there was an easier way to download all the PDF files on a webpage at once? In this tutorial, we’ll show you how to use Python to automate the process of downloading PDF files from a webpage.
🌐
CodeConvert AI
codeconvert.ai › home › code to pdf › python to pdf
Python to PDF - Export Python Code with Syntax Highlighting | Free Online
Free online Python to PDF converter. Export your Python code with syntax highlighting, line numbers, and custom themes. Download as a formatted PDF instantly.
🌐
FreeBookCentre
freebookcentre.net › Language › Free-Python-Books-Download.html
Free Python Books Download | Ebooks Online Read books PDF
This book explains the following topics: Introduction and Review, variables, Expressions, Operators, For Loops, Range For Loops, Python Functions : Karel functions, function Analogy, Function as python Objects and Variable Scope. ... This PDF Python for Everybody by Dr.
🌐
Techprofree
techprofree.com › home › python beginner to advanced pdf — free download 2026
Python Beginner to Advanced PDF — Free Download 2026 - Techprofree
June 14, 2026 - Available completely free on the author’s official website. It teaches Python through practical automation — web scraping, working with Excel and PDF files, and writing scripts that save you hours. Perfect for staying motivated.
🌐
FreeBookCentre
freebookcentre.net › programming-books-download › Python-Programming-Course-Material.html
Python Programming Course Material | Download book PDF
Python Programming Course Material Download Books and Ebooks for free in pdf and online for beginner and advanced levels
🌐
PyPI
pypi.org › project › python-pdf
python-pdf · PyPI
PDF generation in python using wkhtmltopdf suitable for heroku
🌐
Realpython
static.realpython.com › python-basics-sample-chapters.pdf pdf
Python Basics: A Practical Introduction to Python 3
one, Python is open source freeware, meaning you can download it · for free and use it for any purpose, commercial or not. Python also has an amazing community that has built a number of · useful tools that you can use in your own programs. Need to work · with PDF documents?