How to convert a url string to safe characters with python?

后端 未结 3 1839
有刺的猬
有刺的猬 2020-12-10 11:48

Suppose I have the following string:

\"http://earth.google.com/gallery/kmz/women_for_women.kmz?a=large\"

Is there some function or module t

3条回答
  •  庸人自扰
    2020-12-10 11:58

    Python 2's urllib.quote_plus, and Python 3's urllib.parse.quote_plus

    url = "http://earth.google.com/gallery/kmz/women_for_women.kmz?a=large"
    # Python 2
    urllib.quote_plus(url)
    # Python 3
    urllib.parse.quote_plus(url)
    

    outputs:

    'http%3A%2F%2Fearth.google.com%2Fgallery%2Fkmz%2Fwomen_for_women.kmz%3Fa%3Dlarge'
    

提交回复
热议问题