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

后端 未结 3 1831
有刺的猬
有刺的猬 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'
    
    0 讨论(0)
  • 2020-12-10 12:12

    Are you looking for urllib.quote or urllib.quote_plus? Note that you do not quote the entire url string as you mentioned in the question. You normally quote the part in the path or after the query string. Whichever you are going to use in the application.

    0 讨论(0)
  • 2020-12-10 12:17

    Available in the Windows platform

    #! python3.6
    from urllib.parse import quote
    
    
    # result: http://www.oschina.net/search?scope=bbs&q=C%E8%AF%AD%E8%A8%80
    quote('http://www.oschina.net/search?scope=bbs&q=C语言',safe='/:?=&')
    
    0 讨论(0)
提交回复
热议问题