Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render HTML in python?

This code does not work for me

I would like to know a way to render html using python without tkinterhtml

When loading google.com, I get the error:

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

I am working on this for a project and am launching the code using the import keyword. My code doesn't work and closes the window after the search funtion finishes.

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()
like image 790
aanginer Avatar asked Jul 02 '26 21:07

aanginer


2 Answers

tkinter does not have the ability to render HTML. You'll have to use a third-party library. Since you explicitly said you don't want to use tkinterhtml you'll have to find some other third-party renderer.

like image 104
Bryan Oakley Avatar answered Jul 05 '26 10:07

Bryan Oakley


You're decoding the google.com page as 'utf8' or 'utf16'; however, the google.com page uses the ISO-8859-1 encoding:

The header returned by google.com is:

Content-Type: text/html; charset=ISO-8859-1

You will need to decode the page using 'ISO-8859-1' to avoid the "codec can't decode byte 0x.." errors.

like image 36
Jiří Baum Avatar answered Jul 05 '26 11:07

Jiří Baum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!