vinay kusuma
vinay kusuma

Reputation: 75

Download a pdf embedded in webpage using python2.7

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()

Upvotes: 0

Views: 234

Answers (2)

sahasrara62
sahasrara62

Reputation: 11247

from pathlib import Path
import requests
filename = Path("jco.2018.77.8738.pdf")
url = "https://ascopubs.org/doi/pdfdirect/10.1200/JCO.2018.77.8738"
response = requests.get(url)
filename.write_bytes(response.content)

Upvotes: 1

Ardein_
Ardein_

Reputation: 166

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)

Upvotes: 1

Related Questions