urllib.parse.quote
的中文对应是什么?是
urllib.urlencode()
吗?谢谢。
urllib.parse.quote
的中文对应是什么?urllib.urlencode()
吗?我认为你正在寻找 urllib.pathname2url。对比一下:
Python 3中,urllib.parse.quote:
>>> urllib.parse.quote('abc def/foo?bar=baz')
'abc%20def/foo%3Fbar%3Dbaz'
Python 2, urllib.pathname2url:
>>> urllib.pathname2url('abc def/foo?bar=baz')
'abc%20def/foo%3Fbar%3Dbaz'
这种行为在我看来似乎很相似,但它们可能有微小的不同。
编辑:
根据您对Algina帖子的评论,我认为这是我构建URL的首选方式:
>>> url = 'http://dev.echonest.com/api/v4/song/search'
>>> params = {'api_key': 'xxxx', 'format': 'json', 'artist': 'Galaxie 500'}
>>> "{}?{}".format(url, urllib.urlencode(params))
'http://dev.echonest.com/api/v4/song/search?api_key=xxxx&artist=Galaxie+500&format=json'
通过使用专门为Python2/Python3兼容性设计的库six
,您可以实现...
import six.moves.urllib as urllib
# and now you can use urllib as it was python3
urllib.quote(...)
如果您只需要Python2,那么直接使用urllib.quote
即可。
six(1.10.0)
包,但是我遇到了AttributeError:'Module_six_moves_urllib'对象没有属性'quote'
的问题。你有什么想法吗? - Tom Hunterurllib.parse.quote(...)
(实际上在我的情况下我需要使用 urllib.parse.quote_plus(...)
)。 - Tom Huntersix.moves.urllib
命名空间实现了Python3的urllib
命名空间。这样做是为了鼓励在Py2环境中以Py3兼容的方式编写代码。 - cowbert你能更具体一些吗?
就像你提到的那样,你有
urllib.parse.quote_plus(...)
urllib.parse.quote_from_bytes(...)
urllib.parse.unquote(...)
在这里查看文档: https://docs.python.org/3.2/library/urllib.parse.html
req = 'http://dev.echonest.com/api/v4/song/search?api_key=xxxx&format=json&artist=' + urllib.parse.quote(artist)
和artist = "Oasis"
。 - timothylhuillier