quote函数什么意思,怎么用

来源:互联网 发布:百度大数据使用方法 编辑:程序博客网 时间:2024/04/29 20:38

quote函数  属于urllib库里面的一个函数


屏蔽特殊的字符、比如如果url里面的空格!url里面是不允许出现空格的。
按照标准, URL 只允许一部分 ASCII 字符(数字字母和部分符号),其他的字符(如汉字)是不符合 URL 标准的。
所以 URL 中使用其他字符就需要进行 URL 编码。
URL 中传参数的部分(query String),格式是:
name1=value1&name2=value2&name3=value3 
假如你的 name 或者 value 值中有『&』或者『=』等符号,就当然会有问题。所以URL中的参数字符串也需要把『&=』等符号进行编码。
URL编码的方式是把需要编码的字符转化为 %xx 的形式。通常 URL 编码是基于 UTF-8 的


import urllib,os #对字符串进行编码 stra = urllib.quote('this is python') print stra #对字符串进行解码 print urllib.unquote(stra) #这个方法用‘+’代替了%20 和urllib.quote类似, strb = urllib.quote_plus('this is python') print strb #解码 print urllib.unquote_plus(strb)  dicta = {'name':'zeping','passwd':'123456'} #urlencode将字典转换成url参数 print urllib.urlencode(dicta)  #将本地路径转换成url路径 filename = urllib.pathname2url('/python/test.py') print filename #将url路径转换成本地路径 print urllib.url2pathname(filename)  ************ 运行结果************ #对字符串进行编码this%20is%20python #对字符串进行解码 this is python this+is+python this is python passwd=123456&name=zeping /python/test.py /python/test.py 




1 0
原创粉丝点击