import tkinter as tk import pdfkit from bs4 import BeautifulSoup import requests import os options = {'enable-local-file-access': None } url ='http://funds.hexun.com/2020-07-28/201774800.html' def get_title(url): page = requests.get(url) soup = BeautifulSoup(page.content, 'html.parser') title_tag = soup.find('title').text title = title_tag.strip() return title '''将网页生成pdf文件''' def generate_pdf(url): title = get_title(url) pdf_file_name = f"{title}.pdf" # 将wkhtmltopdf.exe程序绝对路径传入config对象 path_wkthmltopdf = r'E:\PDF\wkhtmltopdf\bin\\wkhtmltopdf.exe' config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf) # 生成pdf文件,to_file为文件路径 pdfkit.from_url(url, pdf_file_name,configuration=config,options=options) return pdf_file_name filename = generate_pdf(url) print(f"PDF 文件 {filename} 生成成功!") File "C:\Users\ASUS\PycharmProjects\pythonProject1\venv\lib\site-packages\pdfkit\api.py", line 27, in from_url return r.to_pdf(output_path) File "C:\Users\ASUS\PycharmProjects\pythonProject1\venv\lib\site-packages\pdfkit\pdfkit.py", line 201, in to_pdf self.handle_error(exit_code, stderr) File "C:\Users\ASUS\PycharmProjects\pythonProject1\venv\lib\site-packages\pdfkit\pdfkit.py", line 155, in handle_error raise IOError('wkhtmltopdf reported an error:\n' + stderr) OSError: wkhtmltopdf reported an error: libpng warning: iCCP: known incorrect sRGB profile Exit with code 1 due to network error: HostNotFoundError
时间: 2023-06-04 11:06:33 浏览: 216
这段代码使用Python编程语言编写,主要用到了tkinter、pdfkit、BeautifulSoup和requests等库。代码目的是将指定网页转换为PDF文件并保存在本地。通过获取网页标题作为文件名,生成PDF文件。其中包括了“enable-local-file-access”参数,用于允许访问本地文件。具体实现方式是先通过requests库获取网页内容,再利用BeautifulSoup库对网页内容进行解析,提取网页标题。最后利用pdfkit库生成和保存PDF文件。该段代码的网页链接是http://funds.hexun.com/2020-07-28/201774800.html。
相关问题
import tkinter as tk import requests from bs4 import BeautifulSoup def scan(): url = entry.get() res = requests.get(url) soup = BeautifulSoup(res.text, 'html.parser') links = soup.find_all('a') for link in links: print(link.get('href')) root = tk.Tk() root.title('Web Scanner') label = tk.Label(root, text='Enter URL:') label.pack() entry = tk.Entry(root) entry.pack() button = tk.Button(root, text='Scan', command=scan) button.pack() root.mainloop() 帮我写一个注释
# 导入必要的模块
import tkinter as tk
import requests
from bs4 import BeautifulSoup
# 定义一个函数,用于扫描网页并输出链接
def scan():
# 获取用户输入的网址
url = entry.get()
# 发送请求并获取响应内容
res = requests.get(url)
# 使用 BeautifulSoup 解析响应内容
soup = BeautifulSoup(res.text, 'html.parser')
# 查找所有的链接标签
links = soup.find_all('a')
# 遍历所有的链接标签并输出链接
for link in links:
print(link.get('href'))
# 创建一个窗口对象
root = tk.Tk()
# 设置窗口标题
root.title('Web Scanner')
# 创建一个标签对象并添加到窗口中
label = tk.Label(root, text='Enter URL:')
label.pack()
# 创建一个文本框对象并添加到窗口中
entry = tk.Entry(root)
entry.pack()
# 创建一个按钮对象并添加到窗口中
button = tk.Button(root, text='Scan', command=scan)
button.pack()
# 进入消息循环,等待用户操作
root.mainloop()
阅读全文