Python的url编码函数使用的一个小问题

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

编码:urllib.quote(string[, safe]),除了三个符号“_.-”外,将所有符号编码,后面的参数safe是不编码的字符,使用的时候如果不设置的话,会将斜杠,冒号,等号,问号都给编码了。

如下:

  1. >>> import urllib 
  2. >>> print urllib.quote("http://neeao.com/index.php?id=1"
  3. http%3A//neeao.com/index.php%3Fid%3D1 

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

设置下不编码的符号:

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

这下就好了。

Tags: python

上一篇: 微盾PHP脚本加密专家解密算法
下一篇: Python批量修改文件后缀脚本

相关文章

访客评论

  1. #1 fledna 2009-09-16, 13:10
    >>> print urllib.quote("http://neeao.com/index.php?id=1",":?=/")
    http://neeao.com/index.php?id=1

    我疑问...你quote 干嘛? 闲得?
    折腾成这样?
  2. #2 b4 2009-09-19, 14:33
    直接raw_input()提交过来,然后urlopen(),没看出使用urllib.quote()的意义
    ^_^

发表评论

评论内容 (必填):