Ad

Download A Pdf Embedded In Webpage Using Python2.7

- 1 answer

I want to download the pdf and store it in a folder on my local computer. Following is the link of pdf i want to download https://ascopubs.org/doi/pdfdirect/10.1200/JCO.2018.77.8738

I have written code in both python selenium and using urllib but both failed to download.

import time, urllib
time.sleep(2)
pdfPath = "https://ascopubs.org/doi/pdfdirect/10.1200/JCO.2018.77.8738"
pdfName = "jco.2018.77.8738.pdf"
f = open(pdfName, 'wb')
f.write(urllib.urlopen(pdfPath).read())
f.close()
Ad

Answer

It's much easier with requests

import requests 

url = 'https://ascopubs.org/doi/pdfdirect/10.1200/JCO.2018.77.8738'
pdfName = "./jco.2018.77.8738.pdf"
r = requests.get(url)

with open(pdfName, 'wb') as f:
    f.write(r.content)
Ad
source: stackoverflow.com
Ad