如何在Python中使用Urlencode查询字符串?

发布于 2021-02-02 23:14:50

我尝试在提交之前对该字符串进行urlencode

queryString = 'eventName=' + evt.fields["eventName"] + '&' + 'eventDescription=' + evt.fields["eventDescription"]; 
关注者
0
被浏览
73
1 个回答
  • 面试哥
    面试哥 2021-02-02
    为面试而生,有面试问题,就找面试哥。

    你需要将参数传递urlencode()为映射(dict)或2元组序列,例如:

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

    Python 3或以上

    采用:

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

    请注意,这在通常意义上不会进行url编码(请看输出)。为此使用urllib.parse.quote_plus



  • 面试哥
    面试哥 2021-02-02
    为面试而生,有面试问题,就找面试哥。

    Python 2

    你正在寻找的是urllib.quote_plus

    >>> urllib.quote_plus('string_of_characters_like_these:$#@=?%^Q^$')
    '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
    urllib.parse.quote_plus(...)
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看