Dra*_*yer 4 html python pdf download python-3.x
我想制作一个可以将网站下载为 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,以便可以下载,但我不知道如何执行此操作。任何帮助是极大的赞赏。
小智 7
首先是方法
from_url from module 'pdfkit'
Run Code Online (Sandbox Code Playgroud)
True
调用时返回。
执行此行后,pdf = pdfkit.from_url(url, "file.pdf")
值pdf
是True
或False
取决于下载和创建文件。
因此该行
r = requests.get(pdf)
被评估为
r = requests.get(True)
Which can't not be加重。
基本上你只需要向用户询问文件的 url 和路径
url = input("Please enter the url of the file you want to download.")
path = input("Please enter the file path ex. C:\Jim\Desktop")
file_name = input("Please enter file name")
if pdfkit.from_url(str(url), str(path + file_name)): # Check if method from_url returned True
print("Sucessfully created pdf from url")
else:
print("Something went wrong")
Run Code Online (Sandbox Code Playgroud)