anny
anny

Reputation: 73

How to get response data of a pdf file in python

I am trying to fetch the data of a pdf file available online I have tried

import requests
response = requests.get("http://imdagrimet.gov.in/sites/default/files/daas_bulletin/District%20Advisory%20patna_17.pdf")
print(response.content)

but it gives a byte object as a response, and I am not able to decode that

Upvotes: 0

Views: 2273

Answers (2)

Dmitry Shevchenko
Dmitry Shevchenko

Reputation: 478

Try to write your data to file:

import requests
import shutil


url = 'your url'
r = requests.get(url, stream=True)
if r.status_code == 200:
    with open(file_path, 'wb') as f:
        r.raw.decode_content = True
        shutil.copyfileobj(r.raw, f)

Upvotes: 0

Maaz
Maaz

Reputation: 2445

You should write the data inside a file in order to be able to get it.

Like this:

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

Upvotes: 1

Related Questions