Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在python中对URL进行URL编码_Python_Urlencode_Urllib - Fatal编程技术网

无法在python中对URL进行URL编码

无法在python中对URL进行URL编码,python,urlencode,urllib,Python,Urlencode,Urllib,为什么在尝试对该字符串进行URL编码时会出现此错误 >>> callback = "http://localhost/application/authtwitter?twitterCallback" >>> urllib.urlencode(callback) Traceback (most recent call last): File "<stdin>", line 1, in <module> Fil

为什么在尝试对该字符串进行URL编码时会出现此错误

 >>> callback = "http://localhost/application/authtwitter?twitterCallback"
 >>> urllib.urlencode(callback)
 Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/lib/python2.7/urllib.py", line 1261, in urlencode
      raise TypeError
 TypeError: not a valid non-string sequence or mapping object
>>回调=”http://localhost/application/authtwitter?twitterCallback"
>>>urllib.urlencode(回调)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
urlencode中的文件“/usr/lib/python2.7/urllib.py”,第1261行
提高打字错误
TypeError:不是有效的非字符串序列或映射对象

Python不是PHP。您希望这样做。

该函数不是这样做的:

urlencode(query, doseq=0)
    Encode a sequence of two-element tuples or dictionary into a URL query string.
你在找什么

  • urllib.quote(回调)
    Python2
  • urllib.parse.quote(回调)
    Python3
    • urlencode()

      此函数仅编码两个元素元组或字典

        >>> import urllib  
        >>> dict = { 'First_Name' : 'Jack', 'Second_Name' : "West"}
        >>> urllib.urlencode(dict)
        'First_Name=Jack&Second_Name=West
      
      quote_plus()

      此函数用于对url字符串进行编码

        >>> import urllib   
        >>> url ="https://www.google.com/"
        >>> urllib.quote_plus(url)
        'https%3A%2F%2Fwww.google.com%2F'
      
      Python 3 在Python 3中,quote和urlencode功能位于模块中

      一些例子:

      (联合国)报价 urllib.parse. 使用%xx转义替换字符串中的特殊字符

      类似地,要反转此操作,请使用urllib.parse.unquote:

      urllib.parse. 将%xx转义替换为它们的等效单字符

      (联合国)报价加 urllib.parse. 与quote()类似,但也将空格替换为加号,这是在构建要进入URL的查询字符串时引用HTML表单值所必需的

      类似地,要反转此操作,请使用urllib.parse.unquote_plus:

      urllib.parse. 与unquote()类似,但也将加号替换为空格,这是取消引用HTML表单值所必需的

      URL编码 urllib.parse.
      这对我很有帮助。它让我看得更近了。因为我来自php。这个答案只适用于Python 2。对于Python 3,它是urllib.parse.quote。你能添加一个附录吗?太好了,救了我一天
      >>> import urllib.parse
      >>> urllib.parse.quote('https://www.google.com/')
      'https%3A//www.google.com/'
      
      >>> import urllib.parse
      >>> urllib.parse.unquote('https%3A//www.google.com/')
      'https://www.google.com/'
      
      >>> import urllib.parse
      >>> urllib.parse.quote_plus('https://www.google.com/')
      'https%3A%2F%2Fwww.google.com%2F'
      
      >>> import urllib.parse
      >>> urllib.parse.unquote_plus('https%3A%2F%2Fwww.google.com%2F')
      'https://www.google.com/'
      
      >>> import urllib.parse
      >>> d = {'url': 'https://google.com', 'event': 'someEvent'}
      >>> urrlib.parse.urlencode(d)
      'url=https%3A%2F%2Fgoogle.com&event=someEvent'