小编Dra*_*yer的帖子

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

我想制作一个可以将网站下载为 PDF 的脚本。用户应该能够输入 URL ( https://stackoverflow.com/ ) 和 PDF 下载到的文件路径 (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)
Run Code Online (Sandbox Code Playgroud)

由于某种原因,PDF 无法下载。我想我需要首先将网页转换为 HTML,然后将其转换为 PDF,以便可以下载,但我不知道如何执行此操作。任何帮助是极大的赞赏。

html python pdf download python-3.x

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

download ×1

html ×1

pdf ×1

python ×1

python-3.x ×1