使用python2.7下载嵌入在网页中的pdf

时间:2019-03-20 07:34:41

标签: python python-2.7 selenium

我想下载pdf并将其存储在本地计算机上的文件夹中。 以下是我要下载https://ascopubs.org/doi/pdfdirect/10.1200/JCO.2018.77.8738

的pdf链接

我已经用python硒和urllib编写了代码,但是都无法下载。

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

2 个答案:

答案 0 :(得分:1)

处理请求要容易得多

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)

答案 1 :(得分: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)
相关问题