无法在 python 中对 URL 进行 urllib.urlencode 编码
为什么我在尝试对此字符串进行 urlencode 时收到此错误
>>> 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
Why am I getting this error when trying to urlencode this string
>>> 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这不是该函数的作用:
您在寻找吗?
urllib.quote(回调)
Python 2urllib.parse.quote(回调)
Python 3That's not what that function does:
Are you looking for?
urllib.quote(callback)
Python 2urllib.parse.quote(callback)
Python 3Python 不是 PHP。您想要
urllib.quote()
相反。Python is not PHP. You want
urllib.quote()
instead.urlencode()
此函数仅对二元素元组或字典进行编码。
quote_plus()
该函数对 url 字符串进行编码
urlencode()
This function encode two-element tuples or dictionary only.
quote_plus()
This function encode url string
Python 3
在 Python 3 中,引用和 urlencode 功能位于模块
urllib.parse
。一些示例:
(un)引用
urllib.parse。引用
同样,要反转此操作,请使用 urllib.parse.unquote:
urllib.parse.取消引用
(un)quote_plus
urllib.parse。quote_plus< /a>
同样,要反转此操作,请使用 urllib.parse.unquote_plus:
urllib.parse.unquote_plus
urlencode
urllib.parse。urlencode
Python 3
In Python 3, the quote and urlencode functionalities are located in the module
urllib.parse
.Some Examples:
(un)quote
urllib.parse.quote
Similarly, to reverse this operation, use urllib.parse.unquote:
urllib.parse.unquote
(un)quote_plus
urllib.parse.quote_plus
Similarly, to reverse this operation, use urllib.parse.unquote_plus:
urllib.parse.unquote_plus
urlencode
urllib.parse.urlencode