代码之家  ›  专栏  ›  技术社区  ›  shenglih

python解码非英语用作url?

  •  0
  • shenglih  · 技术社区  · 7 年前

    我有一个变量,比如 title :

    title = "révolution_essentielle"
    

    我可以像这样对它进行编码和解码,用于其他目的:

    title1 = unicode(title, encoding = "utf-8")
    

    但是,如何保留非英语并将其作为URL字符串的一部分来访问URL?例如,我理想地想要 https://mainurl.com/révolution_essentielle.html 通过连接多个字符串,包括 标题 这样地:

    url = main_url + "/" + title + ".html"
    

    有人能告诉我怎么做吗?谢谢大家!

    1 回复  |  直到 7 年前
        1
  •  0
  •   Ondrej K.    7 年前

    %

    urllib.quote()

    urllib.quote("révolution_essentielle")
    

    unicode

    urllib.quote(u'hey_there_who_likes_lego_that\xe3\u019\xe2_\xe3_...'.encode('utf8')).
    


    urllib.parse.quote()

    urllib.parse.quote("révolution_essentielle")
    

    str bytes