无法在 python 中对 URL 进行 urllib.urlencode 编码

发布于 2024-12-10 03:57:02 字数 430 浏览 5 评论 0原文

为什么我在尝试对此字符串进行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

江湖正好 2024-12-17 03:57:02

这不是该函数的作用:

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

您在寻找吗?

  • urllib.quote(回调) Python 2
  • urllib.parse.quote(回调) Python 3

That's not what that function does:

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

Are you looking for?

  • urllib.quote(callback) Python 2
  • urllib.parse.quote(callback) Python 3
你怎么敢 2024-12-17 03:57:02

Python 不是 PHP。您想要 urllib.quote() 相反。

Python is not PHP. You want urllib.quote() instead.

秋凉 2024-12-17 03:57:02

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'

urlencode()

This function encode two-element tuples or dictionary only.

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

quote_plus()

This function encode url string

  >>> import urllib   
  >>> url ="https://www.google.com/"
  >>> urllib.quote_plus(url)
  'https%3A%2F%2Fwww.google.com%2F'
凡尘雨 2024-12-17 03:57:02

Python 3

在 Python 3 中,引用和 urlencode 功能位于模块 urllib.parse

一些示例:

(un)引用

urllib.parse。引用

使用 %xx 转义符替换字符串中的特殊字符。

>>> import urllib.parse
>>> urllib.parse.quote('https://www.google.com/')
'https%3A//www.google.com/'

同样,要反转此操作,请使用 urllib.parse.unquote:

urllib.parse.取消引用

将 %xx 转义符替换为其等效的单字符转义符。

>>> import urllib.parse
>>> urllib.parse.unquote('https%3A//www.google.com/')
'https://www.google.com/'

(un)quote_plus

urllib.parse。quote_plus< /a>

与 quote() 类似,但也用加号替换空格,这是在构建要进入 URL 的查询字符串时引用 HTML 表单值的要求。

>>> import urllib.parse
>>> urllib.parse.quote_plus('https://www.google.com/')
'https%3A%2F%2Fwww.google.com%2F'

同样,要反转此操作,请使用 urllib.parse.unquote_plus:

urllib.parse.unquote_plus

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

>>> import urllib.parse
>>> urllib.parse.unquote_plus('https%3A%2F%2Fwww.google.com%2F')
'https://www.google.com/'

urlencode

urllib.parse。urlencode

>>> import urllib.parse
>>> d = {'url': 'https://google.com', 'event': 'someEvent'}
>>> urrlib.parse.urlencode(d)
'url=https%3A%2F%2Fgoogle.com&event=someEvent'

Python 3

In Python 3, the quote and urlencode functionalities are located in the module urllib.parse.

Some Examples:

(un)quote

urllib.parse.quote

Replace special characters in string using the %xx escape.

>>> import urllib.parse
>>> urllib.parse.quote('https://www.google.com/')
'https%3A//www.google.com/'

Similarly, to reverse this operation, use urllib.parse.unquote:

urllib.parse.unquote

Replace %xx escapes by their single-character equivalent.

>>> import urllib.parse
>>> urllib.parse.unquote('https%3A//www.google.com/')
'https://www.google.com/'

(un)quote_plus

urllib.parse.quote_plus

Like quote(), but also replace spaces by plus signs, as required for quoting HTML form values when building up a query string to go into a URL.

>>> import urllib.parse
>>> urllib.parse.quote_plus('https://www.google.com/')
'https%3A%2F%2Fwww.google.com%2F'

Similarly, to reverse this operation, use urllib.parse.unquote_plus:

urllib.parse.unquote_plus

Like unquote(), but also replace plus signs by spaces, as required for unquoting HTML form values.

>>> import urllib.parse
>>> urllib.parse.unquote_plus('https%3A%2F%2Fwww.google.com%2F')
'https://www.google.com/'

urlencode

urllib.parse.urlencode

>>> import urllib.parse
>>> d = {'url': 'https://google.com', 'event': 'someEvent'}
>>> urrlib.parse.urlencode(d)
'url=https%3A%2F%2Fgoogle.com&event=someEvent'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文