urllib.parse quote/unquate/urlencode

  1. quote :url编码函数,将中文进行转化为%xxxx
    # _*_ coding::utf_8 _*_
    import urllib.request
    import urllib.parse
    url='http://baidu.com/index.html?name=杨洪&pwd=123456'
    ret=urllib.parse.quote(url)
    ret1=urllib.parse.unquote(ret)
    print(ret)
    print(ret1)
    
    
    ##输出结果:
    http%3A//baidu.com/index.html%3Fname%3D%E6%9D%A8%E6%B4%AA%E8%BE%89%26pwd%3D123456
    
    
    http://baidu.com/index.html?name=杨洪&pwd=123456

     

  2. unquote:url解码函数,将%XXX转化为指定字符
  3. urllencode:给定一个字典,将字典拼接为query_string,并且实现了编码功能
    # _*_ coding::utf_8 _*_
    import urllib.parse
    url='http://baidu.com/index.html'
    data={
        'name':'goudan',
        'sex':'',
         'age':'18'
    }
    query_string=urllib.parse.urlencode(data)
    print(query_string)
    url=url+"?"+query_string
    print(url)
    ####
    运行结果
    name=goudan&sex=%E5%A5%B3&age=18
    http://baidu.com/index.html?name=goudan&sex=%E5%A5%B3&age=18
    #用于url的拼接

     

posted on 2019-09-21 21:23  ybl20000418  阅读(319)  评论(0编辑  收藏  举报

导航