Vamshi Kolanu
Vamshi Kolanu

Reputation: 409

unable to dowload pdf using python

I am trying to download a pdf using python script. I had tried using urlib, pdfkit and also curl. While I am trying to download the pdf, I am getting html/js content of the page instead of the pdf file. Kindly help me to solve this issue.

using pdfkit:

import pdfkit
pdfkit.from_url('http://www.kubota.com/product/BSeries/B2301/pdf/B01_Specs.pdf', 'out.pdf', options = {'javascript-delay':'10000'})

using urllib:

import urllib2
response = urllib2.urlopen('http://www.kubota.com/product/BSeries/B2301/pdf/B01_Specs.pdf')
file = open("out.pdf", 'wb')
file.write(response.read())
file.close()

Upvotes: 1

Views: 298

Answers (2)

slearner
slearner

Reputation: 672

You should be able to do it with requests pretty easily

import requests

r = requests.get('http://www.axmag.com/download/pdfurl-guide.pdf') #your url here
with open('your_file_path_here.pdf', 'wb') as f:
    f.write(r.content)

Upvotes: 0

eyllanesc
eyllanesc

Reputation: 243955

You could use the urllib3 library

import urllib3

def download_file(download_url):
    http = urllib3.PoolManager()
    response = http.request('GET', download_url)
    f = open('output.pdf', 'wb')
    f.write(response.data)
    f.close()

if __name__ == '__main__':
    download_file('http://www.kubota.com/product/BSeries/B2301/pdf/B01_Specs.pdf')

Upvotes: 2

Related Questions