pdf_path = "C:\Users\Administrator\PycharmProjects\pythonProject1\history1\zoom\PDF\1.pdf" 地址报错
时间: 2023-11-06 20:05:31 浏览: 139
这个地址报错可能是因为反斜杠被转义了。你可以将地址中的反斜杠改为双反斜杠或者使用正斜杠,例如:
pdf_path = "C:/Users/Administrator/PycharmProjects/pythonProject1/history1/zoom/PDF/1.pdf"
或者
pdf_path = "C:\\Users\\Administrator\\PycharmProjects\\pythonProject1\\history1\\zoom\\PDF\\1.pdf"
相关问题
Python path configuration: PYTHONHOME = (not set) PYTHONPATH = ‘C:\Users\lenovo\PycharmProjects\pythonProject’ program name = ‘C:\Users\lenovo.conda\envs\tensorflow_gpu\python.exe’ isolated = 0 environment = 1 user site = 1 import site = 1 sys._base_executable = ‘C:\Users\lenovo\.conda\envs\tensorflow_gpu\python.exe’ sys.base_prefix = ‘’ sys.base_exec_prefix = ‘’ sys.platlibdir = ‘lib’ sys.executable = ‘C:\Users\lenovo\.conda\envs\tensorflow_gpu\python.exe’ sys.prefix = ‘’ sys.exec_prefix = ‘’ sys.path = [ ‘C:\Users\lenovo\PycharmProjects\pythonProject’, ‘C:\Users\lenovo\.conda\envs\tensorflow_gpu\python39.zip’, ‘C:\Users\lenovo\.conda\envs\tensorflow_gpu’, ] Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding Python runtime state: core initialized ModuleNotFoundError: No module named ‘encodings’ Current thread 0x00000950 (most recent call first): <no Python frame>
这段信息是Python环境配置方面的错误信息,其中最后一行出现了ModuleNotFoundError: No module named 'encodings',这是因为Python无法找到名为'encodings'的模块。建议您检查Python的安装和环境变量是否设置正确,或者重新安装Python。如果问题仍然存在,您可以尝试在Python安装目录中手动创建一个名为'encodings'的空文件夹,然后再次运行程序。
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
这段代码使用Python编程语言编写,主要用到了tkinter、pdfkit、BeautifulSoup和requests等库。代码目的是将指定网页转换为PDF文件并保存在本地。通过获取网页标题作为文件名,生成PDF文件。其中包括了“enable-local-file-access”参数,用于允许访问本地文件。具体实现方式是先通过requests库获取网页内容,再利用BeautifulSoup库对网页内容进行解析,提取网页标题。最后利用pdfkit库生成和保存PDF文件。该段代码的网页链接是http://funds.hexun.com/2020-07-28/201774800.html。
阅读全文