Python Forum

Full Version: Which codec can help me decode the html source?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I want to show a web page(actually a google map) in tkinter
Error:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa0 in position 12619: invalid start byte
import urllib.request
from tk_html_widgets import HTMLLabel
from tkinter import *
root = tk.Tk()
text = HTMLLabel(root)
text.pack(fill="both", expand=True)
text.set_html(urllib.request.urlopen("https://www.google.com" ).read().decode("UTF-8"))
text.fit_height()
root.mainloop()
You need to take a few tutorials before attempting this.
Search YouTube for tkinter canvas tutorial
I have tried a lot but I am unable to open the google map in tkinter. help me with it. I went through but still I was unable to solve the error[Image: qlh3hb]

[Image: qlh5ap]


import tkinter
from gmplot import *
from tk_html_widgets import HTMLLabel
root = tkinter.Tk()
gmap = gmplot.GoogleMapPlotter(28.5355, 77.3910, 18)
#gmap.apikey = "AIzaSyDeRNMnZ__VnQDiATiuz4kPjF_c9r1kWe8"
gmap.draw( "map_key.html" )
frame = tkinter.Frame(root, height = 200, width = 200, bg = "yellow").pack()
html = open("map_key.html", "r").read()
html_label = HTMLLabel(frame, html=html, height = 100)
html_label.pack(fill="both", expand=True)
html_label.fit_height()
root.mainloop()
(Jan-09-2020, 05:31 PM)vivekagrey Wrote: [ -> ]I want to show a web page(actually a google map) in tkinter
Error:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa0 in position 12619: invalid start byte

The encoding is iso-8859-1, better known as latin1.
Not all webpages are encoded with utf8. You need to get this information
from headers or you just use the requests module.


import urllib.request
from tk_html_widgets import HTMLLabel
from tkinter import Tk


root = Tk()
text = HTMLLabel(root)
text.pack(fill="both", expand=True)
req = urllib.request.urlopen("https://www.google.com" )
encoding = req.headers.get_content_charset()
text.set_html(req.read().decode(encoding))
text.fit_height()
root.mainloop()
But this is not the complete solution.
I guess you need something which is also able to parse and execute javascript.
If you run this code, you see all JavaScripts in clear text.