URL encoding in python

后端 未结 3 1459
余生分开走
余生分开走 2020-12-08 02:53

Is there a simple method I\'m missing in urllib or other library for this task? URL encoding replaces unsafe ASCII characters with a \"%\" followed by two hexa

3条回答
  •  再見小時候
    2020-12-08 03:47

    For Python 2.x, use urllib.quote

    Replace special characters in string using the %xx escape. Letters, digits, and the characters '_.-' are never quoted. By default, this function is intended for quoting the path section of the URL. The optional safe parameter specifies additional characters that should not be quoted — its default value is '/'.

    example:

    In [1]: import urllib
    
    In [2]: urllib.quote('%')
    Out[2]: '%25'
    

    EDIT:

    In your case, in order to replace space by plus signs, you may use urllib.quote_plus

    example:

    In [4]: urllib.quote_plus('a b')
    Out[4]: 'a+b'
    

    For Python 3.x, use quote

    >>> import urllib
    >>> a = "asdas#@das"
    >>> urllib.parse.quote(a)
    'asdas%23%40das'
    

    and for string with space use quote_plus

    >>> import urllib
    >>> a = "as da& s#@das"
    >>> urllib.parse.quote_plus(a)
    'as+da%26+s%23%40das'
    

提交回复
热议问题