如何使用Python将网页下载为PDF?

时间:2019-01-27 16:42:54

标签: python python-3.x pdf download webpage

我想制作一个脚本,可以将网站下载为PDF。用户应该能够输入要下载的PDF的URL(https://stackoverflow.com/)和文件路径(c:\ Bob \ PDF)。

到目前为止,这是我的代码:

import requests
import pdfkit

url = input("Please enter the url of the file you want to download.")
pdf = pdfkit.from_url(url, "file.pdf")
path = input("Please enter the file path that you would like the file to 
download to. c:\Bob\PDF is an example of a valid file path.")

print("Download starting.")
r = requests.get(pdf)

with open(path, 'wb') as f:
    f.write(r.content)

由于某些原因,PDF无法下载。我认为我需要先将网页转换为HTML,然后再将其转换为PDF,以便可以下载,但是我不确定该如何进行。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

首先是方法

from_url from module 'pdfkit' 

在被调用时返回True

执行此行后,pdf = pdfkit.from_url(url, "file.pdf")的{​​{1}}的值为pdfTrue,具体取决于下载和创建文件的情况。

所以这行 False 被评估为 r = requests.get(pdf) 无法正确执行。

基本上,您只需要询问用户文件的网址和路径

r = requests.get(True)