在python中urllib模塊提供上層接口,能夠使用它下載讀取數據,這裏舉個例子,把sina首頁的html抓取下來顯示出來.有2種方法能夠實現. html
1.urlopen(url, data=None, proxies=None)
urlopen(url [, data]) -> open file-like object python
建立一個表示遠程url的類文件對象,而後像本地文件同樣操做這個類文件對象來獲取遠程數據。參數url表示遠程數據的路徑,通常是網址;參數data表示以post方式提交到url的數據;參數proxies用於設置代理.urlopen返回一個類文件對象. shell
#!/usr/bin/python2.5 import urllib url = "http://www.sina.com" data = urllib.urlopen(url).read() print data
root@10.1.6.200:~# python gethtml.py <!Doctype html> <!--[30,131,1] published at 2013-04-11 23:15:33 from #150 by system--> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=gb2312" /> <title>тK˗ҳ</title> <meta name="keywords" content="тK,тKθ,SINA,sina,sina.com.cn,тK˗ҳ,ą»§,؊Ѷ" /> ....
2 urlretrieve(url, filename=None, reporthook=None, data=None) 小程序
urlretrieve方法直接將遠程數據下載到本地。參數filename指定了保存到本地的路徑(若是未指定該參數,urllib會生成一個臨時文件來保存數據);參數reporthook是一個回調函數,當鏈接上服務器.以及相應的數據塊傳輸完畢的時候會觸發該回調. 服務器
#!/usr/bin/python2.5 import urllib url = "http://www.sina.com" path = "/root/sina.txt" data = urllib.urlretrieve(url,path)
root@10.1.6.200:~# python getsina.py root@10.1.6.200:~# cat sina.txt <!Doctype html> <!--[30,131,1] published at 2013-04-11 23:25:30 from #150 by system--> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=gb2312" /> <title>тK˗ҳ</title> <meta name="keywords" content="тK,тKθ,SINA,sina,sina.com.cn,тK˗ҳ,ą»§,؊Ѷ" /> ....
不只如此,這裏寫個爬蟲小程序,能夠把百度貼吧http://tieba.baidu.com/p/2236567282網頁上的jpg圖片依次下載下來. 函數
root@10.1.6.200:~# cat getJpg.py #!/usr/bin/python2.5 import re import urllib def getHtml(url): html = urllib.urlopen(url).read() return html def getJpg(html): reg = r'src="(http://.*?\.jpg)"' imgre = re.compile(reg) imgList = re.findall(imgre,html) x = 0 for imgurl in imgList: urllib.urlretrieve(imgurl,'%s.jpg' % x) x += 1 html = getHtml("http://tieba.baidu.com/p/2236567282") getJpg(html)
root@10.1.6.200:~# python 11.py root@10.1.6.200:~# ls -l total 1680 -rw-r--r-- 1 root root 38695 2013-04-11 23:32 0.jpg -rw-r--r-- 1 root root 48829 2013-04-11 23:32 10.jpg -rw-r--r-- 1 root root 51835 2013-04-11 23:32 11.jpg -rw-r--r-- 1 root root 41688 2013-04-11 23:32 12.jpg -rw-r--r-- 1 root root 1077 2013-04-11 23:32 13.jpg -rw-r--r-- 1 root root 33989 2013-04-11 23:32 14.jpg -rw-r--r-- 1 root root 41890 2013-04-11 23:32 15.jpg -rw-r--r-- 1 root root 35728 2013-04-11 23:32 16.jpg -rw-r--r-- 1 root root 44405 2013-04-11 23:32 17.jpg -rw-r--r-- 1 root root 29847 2013-04-11 23:32 18.jpg -rw-r--r-- 1 root root 44607 2013-04-11 23:32 19.jpg -rw-r--r-- 1 root root 23939 2013-04-11 23:32 1.jpg -rw-r--r-- 1 root root 45592 2013-04-11 23:32 20.jpg -rw-r--r-- 1 root root 60910 2013-04-11 23:32 2.jpg -rw-r--r-- 1 root root 39014 2013-04-11 23:32 3.jpg -rw-r--r-- 1 root root 19057 2013-04-11 23:32 4.jpg -rw-r--r-- 1 root root 64584 2013-04-11 23:32 5.jpg -rw-r--r-- 1 root root 29297 2013-04-11 23:32 6.jpg -rw-r--r-- 1 root root 39145 2013-04-11 23:32 7.jpg -rw-r--r-- 1 root root 1059 2013-04-11 23:32 8.jpg -rw-r--r-- 1 root root 44797 2013-04-11 23:32 9.jpg