Decoding | Python
Learn to decode Base64-encoded binary responses from ScraperAPI Async in Python. Process PDFs, images, and files with Python and command-line examples.
The responses returned by the Async API for binary requests require you to decode the data as they are encoded using Base64
encoding. This allows the binary data to be sent as a text string, which can then be decoded back into its original form when you want to use it.
Example request:
import requests
r = requests.post(url = 'https://async.scraperapi.com/jobs', json={ 'apiKey': 'API_KEY', 'url': 'https://pdfobject.com/pdf/sample.pdf' })
print(r.text)
Decode response:
import requests
import base64
r = requests.get('https://async.scraperapi.com/jobs/<JOB_ID>')
base64_encoded_body = r.json()['response']['base64EncodedBody']
pdf_data = base64.b64decode(base64_encoded_body)
with open('name.pdf', 'wb') as f:
f.write(pdf_data)
Last updated
Was this helpful?