[python]代码库
1 #! /usr/bin/env python
2
3 import sys,urllib
4 def reporthook(*a): print a #reporthook函数会在每块数据下载或传输完成后被调用,
#注释:用(block number, block size, total size)这三个参数调用reporthook函数。
5 for url in sys.argv[1:]:
6 i = url.rfind('/')
7 file = url[i+1:]
8 print url,"-->",file
9 urllib.urlretrieve(url,file,reporthook) #返回二元组(filename, mime_hdrs)
10
===============================
运行结果:
[root@dogood pycode]# python wget.py http://www.python.org/doc/FAQ.html
http://www.python.org/doc/FAQ.html --> FAQ.html
(0, 8192, 6788)
(1, 8192, 6788)
[root@dogood pycode]# ll
total 36
drwxr-xr-x 2 root root 4096 Jan 27 04:24 chat
drwxr-xr-x 2 root root 4096 Jan 31 06:30 core16
-rw-r--r-- 1 root root 6788 Feb 1 04:27 FAQ.html #下载的网页
-rw-r--r-- 1 root root 209 Feb 1 04:27 wget.py
[root@dogood pycode]#