Best way to convert a Unicode URL to ASCII (UTF-8 percent-escaped) in Python?

前端 未结 5 1520
情话喂你
情话喂你 2020-12-12 13:30

I\'m wondering what\'s the best way -- or if there\'s a simple way with the standard library -- to convert a URL with Unicode chars in the domain name and path to the equiva

5条回答
  •  被撕碎了的回忆
    2020-12-12 14:05

    You might use urlparse.urlsplit instead, but otherwise you seem to have a very straightforward solution, there.

    protocol, domain, path, query, fragment = urlparse.urlsplit(url)
    

    (You can access the domain and port separately by accessing the returned value's named properties, but as port syntax is always in ASCII it is unaffected by the IDNA encoding process.)

提交回复
热议问题