如何在Python中对查询字符串进行urlencode?
P粉245489391
P粉245489391 2023-08-23 14:36:44
0
2
393
<p>我正在尝试在提交之前对该字符串进行 urlencode。 </p> <pre class="brush:php;toolbar:false;">queryString = 'eventName=' + evt.fields["eventName"] + '&' + 'eventDescription=' + evt.fields["eventDescription"];</pre> <p><br /></p>
P粉245489391
P粉245489391

全部回复(2)
P粉270891688

Python 2

您要查找的是 urllib.quote_plus

safe_string = urllib.quote_plus('string_of_characters_like_these:$#@=?%^Q^$')

#Value: 'string_of_characters_like_these%3A%24%23%40%3D%3F%25%5EQ%5E%24'

Python 3

在 Python 3 中,urllib 包已被分解为更小的组件。您将使用 urllib.parse.quote_plus (注意 parse 子模块)

import urllib.parse
safe_string = urllib.parse.quote_plus(...)
P粉562845941

您需要将参数传递到 urlencode() 作为映射(字典)或二元组序列,例如:

>>> import urllib
>>> f = { 'eventName' : 'myEvent', 'eventDescription' : 'cool event'}
>>> urllib.urlencode(f)
'eventName=myEvent&eventDescription=cool+event'

Python 3 或更高版本

使用urllib.parse.urlencode

>>> urllib.parse.urlencode(f)
eventName=myEvent&eventDescription=cool+event

请注意,这执行常用意义上的 url 编码(查看输出)。为此,请使用 urllib.parse.quote_plus代码>.

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!