前面介绍了 urllib 模块,以及它常用的 urlopen() 和 urlretrieve()函数的使用介绍。当然 urllib 还有一些其它很有用的辅助方法,比如对 url 进行编码、解码等等。接下来我们再大概介绍一下。

  我们知道,url 中是不能出现一些特殊的符号的,有些符号有特殊的用途。比如以 get 方式提交数据的时候,会在 url 中添加 key=value 这样的字符串,所以在 value 中是不允许有 '=',因此要对其进行编码;与此同时服务器接收到这些参数的时候,要进行解码,还原成原始的数据。这个时候,这些辅助方法会很有用:

  1.   urllib.quote(string[, safe]):对字符串进行编码。参数 safe 指定了不需要编码的字符;

  2.   urllib.unquote(string) :对字符串进行解码;

  3.   urllib.quote_plus(string [ , safe ] ) :与 urllib.quote 类似,但这个方法用'+'来替换' ',而 quote 用' '来代替' '

  4.   urllib.unquote_plus(string ) :对字符串进行解码;

  5.   urllib.urlencode(query[, doseq]):将dict或者包含两个元素的元组列表转换成url参数。例如 字典{'name': 'dark-bull', 'age': 200}将被转换为"name=dark-bull&age=200"

  6.   urllib.pathname2url(path):将本地路径转换成 url 路径;

  7.   urllib.url2pathname(path):将url路径转换成本地路径;

我们接下来运行一下下面的脚本来加深理解。

import urllib
data = 'name = ~nowamagic+5'
 
data1 = urllib.quote(data)
print data1 # result: name = ~nowamagic+5
print urllib.unquote(data1) # name = ~nowamagic+5
 
data2 = urllib.quote_plus(data)
print data2 # result: name+=+~nowamagic+5
print urllib.unquote_plus(data2)    # name = ~nowamagic+5
 
data3 = urllib.urlencode({ 'name': 'nowamagic-gonn', 'age': 200 })
print data3 # result: age=200&name=nowamagic-gonn
 
data4 = urllib.pathname2url(r'd:/a/b/c/23.php')
print data4 # result: ///D://a/b/c/23.php
print urllib.url2pathname(data4)    # result: D:/a/b/c/23.php
在 Python Shell 里执行的具体情况为:

Python 2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import urllib
>>> data = 'name = ~nowamagic+5'
>>> data1 = urllib.quote(data)
>>> print data1
name = ~nowamagic+5
>>> print urllib.unquote(data1)
name = ~nowamagic+5
>>> data2 = urllib.quote_plus(data)
>>> print data2
name+=+~nowamagic+5
>>> print urllib.unquote_plus(data2)
name = ~nowamagic+5
>>> data3 = urllib.urlencode({ 'name': 'nowamagic-gonn', 'age': 200 })
>>> print data3
age=200&name=nowamagic-gonn
>>> data4 = urllib.pathname2url(r'd:/a/b/c/23.php')
>>> print data4
///D://a/b/c/23.php
>>> print urllib.url2pathname(data4)
D:\a\b\c\23.php

urllib模块的基本使用也比较简单,后面根据使用情况会继续跟进了解。

本文出自http://blog.sina.com.cn/s/blog_b369b20d0101kc05.html