Transferred from: http://zeping.blog.51cto.com/6140112/1143722
Urllib and URLLIB2 are all related modules that accept URL requests, but provide different functionality.
Urllib provides the UrlEncode method for generating a Get query string, and Urllib2 does not.
Urllib:
Web Base:
- Import Urllib
- #打开51cto
- CTO = Urllib.urlopen (' http://www.51cto.com ')
- #打开本地文件: CTO = Urllib.urlopen (url= ' file:/root/python/ulib ')
- #打开ftp: CTO = Urllib.urlopen (url= ' ftp://username: password @ftp address/')
- #读取51cto首页的代码
- Print Cto.read ()
- #获取远程服务器返回的头信息, almost like Curl-i www,51cto.com.
- Print Cto.info ()
- #返回http状态码, 200 indicates success, 404 means URL not found
- Print Cto.getcode ()
- #返回请求的URL
- Print Cto.geturl ()
- #运行结果
- [[email protected] python] #python Ctourl
- 。。。。。。。 Omit Cto.read () function, output too much .....
- Server:tengine #cto. info () return information
- date:wed, Feb ::
- Content-type:text/html
- Connection:close
- Vary:accept-encoding
- Load-balancing:web48
- #cto. GetCode () return information
- http://www. 51cto.com #cto. Geturl () return information
- #urlopen返回的是一个类文件对象, and the way this object is used and the file object's
- #使用方法完全一样.
Encoding and decoding of characters:
- Import Urllib,os
- #对字符串进行编码
- Stra = Urllib.quote (' This is Python ')
- Print Stra
- #对字符串进行解码
- Print Urllib.unquote (stra)
- #这个方法用 ' + ' instead of%20 and urllib.quote similar,
- 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)
- ######### #运行结果 ##########
- [[email protected] python] # Python Quote
- this%20is%20python
- This is python
- this+Is+python
- This is python
- passwd=123456&name=zeping
- /python/test.py
- /python/test.py
Urllib.urlretrieve (): Download
- Import Urllib
- def Schedule (a,b,c):
- "' "
- A: Data blocks that have already been downloaded
- B: Size of the data block
- C: The size of the remote file
- ‘‘‘
- per = 100.0 * A * b/c
- if per > :
- per =
- print '%.2f%% '% per
- #这里以下载缓存插件为例
- url = ' http://fastlnmp.googlecode.com/files/eaccelerator-0.9.6.tar.bz2 '
- #获取文件名, here is the download to the current directory, if you want to download to another directory must
- #须输入绝对路径和文件名字:/root/tools/eaccelerator-0.9.6.tar.bz2
- Local = Url.split ('/') [-1]
- Urllib.urlretrieve (Url,local,schedule)
- ######## #运行结果 ##########
- [[email protected] urllib] # python down
- 0%
- 7.74%
- 15.48%
- 23.22%
- 30.96%
- 38.7%
- 46.44%
- 54.18%
- 61.92%
- 69.66%
- 77.4%
- 85.15%
- 92.89%
- 100%
- [[email protected] urllib] # ls
- Down eaccelerator-0.9. 6.tar.bz2 ulib2
URLLIB2:
Urllib2 can accept an instance of the request class to set the URL request headers,urllib only the URL can be accepted.
Above transfer from: http://zeping.blog.51cto.com/6140112/1143722
[Python]-urllib and URLLIB2 modules