Forum Archive

Invalid url thai language

karawooot

Hello
Help me please

import webbrowser

link1 = 'เราชนะ'
link2= 'google'
thaiUrl = "https://www.{}.com".format(link1)
EnUrl = "https://www.{}.com".format(link2)

webbrowser.open(EnUrl)—>>> ok

webbrowser.open(thaiUrl) —>>> invalid url ,how to fix 🙏🏻🙏🏻

JonB

try this:

thaiUrl = "https://www.{}.com".format(link1.encode('idna').decode('latin1'))

reason: urls are not actually allowed to be in Unicode, they technically must be latin1. However, idna is a method of displaying specially coded latin1 urls as Unicode. The .encode('idna') creates an idna encoded url. However, webbrowser expects a string (which must contain only latin1 characters!), not bytes, hence the decode back to latin1.

There may be a cleaner way to do this, but the above should work.

karawooot

Thank you🙏🏻🙏🏻🙏🏻🙏🏻👍👍