0

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()
Mate Mrše
  • 7,997
  • 10
  • 40
  • 77
vinay kusuma
  • 65
  • 1
  • 9
  • 1
    Possible duplicate of [Download and save PDF file with Python requests module](https://stackoverflow.com/questions/34503412/download-and-save-pdf-file-with-python-requests-module) – sahasrara62 Mar 20 '19 at 07:57
  • I saw that answer, Made this since my URL doesn't have filename ending with extension '.pdf' So i was trying more of selenium python implementations. – vinay kusuma Mar 20 '19 at 07:59
  • I just use your information is given and, with given link and file name to be, just used a solution written there and added a link there and problem solved. – sahasrara62 Mar 20 '19 at 08:02

2 Answers2

1

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)
Ardein_
  • 166
  • 6
1
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)
sahasrara62
  • 10,069
  • 3
  • 29
  • 44