How do i save a python url as a pdf?

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":"//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 = '//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

This article was first published on PyShark , and kindly contributed to python-bloggers. [You can report issue about the content on this page here]
Want to share your content on python-bloggers? click here.

In this tutorial we will explore how to download PDF from URL using Python.

Table of Contents

  • Introduction
  • Download PDF from URL using Python
  • Conclusion

Introduction

A lot of product manuals, instructions, books, and other files with lots of text are mainly available online in PDF format.

Downloading several files manually can be a very time consuming task, so in this tutorial we will focus on the automation of this process.

To continue following this tutorial we will need the following Python library: requests.

Requests is a simple Python library that allows you to send HTTP requests.

If you don’t have it installed, please open “Command Prompt” [on Windows] and install it using the following code:

pip install requests

In this section we will learn how to download an image from URL using Python.

Here, we will assume you have the URL of the specific PDF file [and not just a webpage].

As the first step, we will import the required dependency and define a function we will use to download images, which will have 3 inputs:

  1. url – URL of the specific image
  2. file_name – name for the saved image
  3. headers – the dictionary of HTTP Headers that will be sent with the request
import requests


def download_pdf[url, file_name, headers]:

Now we can send a GET request to the URL along with the headers, which will return a Response [a server’s response to an HTTP request]:

import requests


def download_pdf[url, file_name, headers]:

    #Send GET request
    response = requests.get[url, headers=headers]

If the HTTP request has been successfully completed, we should receive Response code 200 [you can learn more about response codes here].

We are going to check if the response code is 200, and if it is, then we will save the image [which is the content of the request], otherwise we will print out the response code:

import requests


def download_pdf[url, file_name, headers]:

    # Send GET request
    response = requests.get[url, headers=headers]

    # Save the PDF
    if response.status_code == 200:
        with open[file_name, "wb"] as f:
            f.write[response.content]
    else:
        print[response.status_code]

The function to download a PDF from URL is ready and now we just need to define the url, file_name, and headers, and then run the code.

For example, in one of the previous tutorials, we used some sample PDF file, and you can it here.

The URL looks like this:

//pyshark.com/wp-content/uploads/2022/05/merged_all_pages.pdf

You can see that it has the .pdf extension, meaning that this is a URL to a specific PDF file.

We will save this image as ‘file1.pdf’.

For the headers we are only using the User-Agent request header which lets the servers identify the application of the requesting user agent [a computer program representing a person, like a browser or an app accessing the Webpage].

import requests


def download_pdf[url, file_name, headers]:

    # Send GET request
    response = requests.get[url, headers=headers]

    # Save the PDF
    if response.status_code == 200:
        with open[file_name, "wb"] as f:
            f.write[response.content]
    else:
        print[response.status_code]


if __name__ == "__main__":

    # Define HTTP Headers
    headers = {
        "User-Agent": "Chrome/51.0.2704.103",
    }

    # Define URL of an image
    url = "//pyshark.com/wp-content/uploads/2022/05/merged_all_pages.pdf"

    # Define image file name
    file_name = "file1.pdf"

    # Download image
    download_pdf[url, file_name, headers]

Run the code and you should see file1.png created in the same directory as the main.py file with the code:

Conclusion

In this article we explored how to download PDF from URL using Python.

Feel free to leave comments below if you have any questions or have suggestions for some edits and check out more of my Python Programming tutorials.

The post Download PDF from URL using Python appeared first on PyShark.

To leave a comment for the author, please follow the link and comment on their blog: PyShark .

Want to share your content on python-bloggers? click here.

How do I save a webpage as a PDF in Python?

Convert Webpage to PDF using Python.
import pdfkit..
#Define path to wkhtmltopdf.exe..
path_to_wkhtmltopdf = r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'.
#Define url..
url = '//wkhtmltopdf.org/'.
#Point pdfkit configuration to wkhtmltopdf.exe..
config = pdfkit. ... .
#Convert Webpage to PDF..

How do I save a Python file as a PDF?

Approach:.
Import the class FPDF from module fpdf..
Add a page..
Set the font..
Insert a cell and provide the text..
Save the pdf with “. pdf” extension..

How do you download and save a URL in Python?

How to Download a File Using urllib Module in Python.
Import the urllib module into your project..
Use urllib's request. urlretrieve[] method to download a file from a specific URL and save it on your machine..

How do I download a URL from Python?

Import module. import requests..
Get the link or url. url = '//www.facebook.com/favicon.ico' r = requests.get[url, allow_redirects=True].
Save the content with name. open['facebook.ico', 'wb'].write[r.content] save the file as facebook. ... .
Get filename from an URL. To get the filename, we can parse the url..

Chủ Đề