如何在 python 中渲染 HTML?
How to render HTML in python?
此代码对我不起作用
我想知道一种使用 python 不使用 tkinterhtml
来渲染 html 的方法
加载 google.com
时,出现错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "c:\python scripts\project\file.py", line 10, in search
html = htmlBytes.decode("utf8")
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe7 in position 10955: invalid continuation byte
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
return self.func(*args)
File "c:\python scripts\project\file.py", line 12, in search
html = htmlBytes.decode("utf16")
UnicodeDecodeError: 'utf-16-le' codec can't decode byte 0x3e in position 14624: truncated data
我正在为一个项目做这件事,我正在使用 import
关键字启动代码。我的代码不起作用并在搜索功能完成后关闭 window。
import urllib.request
import tkinter
from tkinterhtml import HtmlFrame
def search():
url = urlInput.get()
page = urllib.request.urlopen(url)
htmlBytes = page.read()
try:
html = htmlBytes.decode("utf8")
except:
html = htmlBytes.decode("utf16")
frame.set_content(html)
page.close()
screen = tkinter.Tk()
screen.geometry("700x700")
frame = HtmlFrame(screen, horizontal_scrollbar="auto")
urlInput = tkinter.Entry(screen)
urlInput.grid(column=0,row=0,columnspan=10,rowspan=4,sticky="news")
searchBtn = tkinter.Button(screen,text="search",command=search)
searchBtn.grid(row=0,column=11,sticky="news")
screen.mainloop()
Flask 有一个叫做 render template 的工具,我用它来做我的网站,实现起来也不难....这是一个例子:
from flask import Flask
from flask import render_template
app = Flask(__name__)
#sets app route and renders file
@app.route('/')
def index():
return render_template('index.html')
if __name__ == "__main__":
app.run(host='0.0.0.0', port="any port number here", debug=True)
#debug just refreshes the main file (app.py or something.py) when a change is made
我建议您也查看 Flask 上的文档以全面了解它,我真的希望这能回答您的问题
https://flask.palletsprojects.com/en/2.0.x/
tkinter 没有渲染能力 HTML。您必须使用 third-party 库。由于您明确表示不想使用 tkinterhtml,因此您必须找到其他一些 third-party 渲染器。
我已经使用 tkinterweb
库解决了这个问题。
代码:
import tkinter
from tkinterweb import HtmlFrame
screen = tkinter.Tk()
screen.geometry("700x700")
frame = HtmlFrame(screen, horizontal_scrollbar="auto")
urlInput = tkinter.Entry(screen)
def search():
frame.load_website(urlInput.get())
button = tkinter.Button(screen,text="search",command=search)
frame = HtmlFrame(screen)
urlInput.grid(row=0,column=0,columnspan=2)
button.grid(row=1,column=0)
frame.grid(row=2,column=0)
screen.mainloop()
这是给任何想知道我是如何解决它的人的
您正在将 google.com
页面解码为 'utf8'
或 'utf16'
;但是,google.com
页面使用 ISO-8859-1 编码:
google.com
返回的header是:
Content-Type: text/html; charset=ISO-8859-1
您需要使用 'ISO-8859-1'
解码页面以避免“编解码器无法解码字节 0x..”错误。
此代码对我不起作用
我想知道一种使用 python 不使用 tkinterhtml
来渲染 html 的方法加载 google.com
时,出现错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "c:\python scripts\project\file.py", line 10, in search
html = htmlBytes.decode("utf8")
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe7 in position 10955: invalid continuation byte
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
return self.func(*args)
File "c:\python scripts\project\file.py", line 12, in search
html = htmlBytes.decode("utf16")
UnicodeDecodeError: 'utf-16-le' codec can't decode byte 0x3e in position 14624: truncated data
我正在为一个项目做这件事,我正在使用 import
关键字启动代码。我的代码不起作用并在搜索功能完成后关闭 window。
import urllib.request
import tkinter
from tkinterhtml import HtmlFrame
def search():
url = urlInput.get()
page = urllib.request.urlopen(url)
htmlBytes = page.read()
try:
html = htmlBytes.decode("utf8")
except:
html = htmlBytes.decode("utf16")
frame.set_content(html)
page.close()
screen = tkinter.Tk()
screen.geometry("700x700")
frame = HtmlFrame(screen, horizontal_scrollbar="auto")
urlInput = tkinter.Entry(screen)
urlInput.grid(column=0,row=0,columnspan=10,rowspan=4,sticky="news")
searchBtn = tkinter.Button(screen,text="search",command=search)
searchBtn.grid(row=0,column=11,sticky="news")
screen.mainloop()
Flask 有一个叫做 render template 的工具,我用它来做我的网站,实现起来也不难....这是一个例子:
from flask import Flask
from flask import render_template
app = Flask(__name__)
#sets app route and renders file
@app.route('/')
def index():
return render_template('index.html')
if __name__ == "__main__":
app.run(host='0.0.0.0', port="any port number here", debug=True)
#debug just refreshes the main file (app.py or something.py) when a change is made
我建议您也查看 Flask 上的文档以全面了解它,我真的希望这能回答您的问题 https://flask.palletsprojects.com/en/2.0.x/
tkinter 没有渲染能力 HTML。您必须使用 third-party 库。由于您明确表示不想使用 tkinterhtml,因此您必须找到其他一些 third-party 渲染器。
我已经使用 tkinterweb
库解决了这个问题。
代码:
import tkinter
from tkinterweb import HtmlFrame
screen = tkinter.Tk()
screen.geometry("700x700")
frame = HtmlFrame(screen, horizontal_scrollbar="auto")
urlInput = tkinter.Entry(screen)
def search():
frame.load_website(urlInput.get())
button = tkinter.Button(screen,text="search",command=search)
frame = HtmlFrame(screen)
urlInput.grid(row=0,column=0,columnspan=2)
button.grid(row=1,column=0)
frame.grid(row=2,column=0)
screen.mainloop()
这是给任何想知道我是如何解决它的人的
您正在将 google.com
页面解码为 'utf8'
或 'utf16'
;但是,google.com
页面使用 ISO-8859-1 编码:
google.com
返回的header是:
Content-Type: text/html; charset=ISO-8859-1
您需要使用 'ISO-8859-1'
解码页面以避免“编解码器无法解码字节 0x..”错误。