urllib.quote 分类: python python基础学习 2013-07-07 10:19 3436人阅读 评论(0) 收藏

ython的url编码函数是在类urllib库中,使用方法是:

编码:urllib.quote(string[, safe]),除了三个符号“_.-”外,将所有符号编码,后面的参数safe是不编码的字符,

使用的时候如果不设置的话,会将斜杠,冒号,等号,问号都给编码了。


如下:

>>> import urllib

>>> print urllib.quote("http://neeao.com/index.php?id=1")

http%3A//neeao.com/index.php%3Fid%3D1

这样在使用urllib.urlopen打开编码后的网址的时候,就会报错了。


设置不编码的符号:

>>> print urllib.quote("http://neeao.com/index.php?id=1",":?=/")

http://neeao.com/index.php?id=1 这下就好了。

原文地址:https://www.cnblogs.com/think1988/p/4628132.html