API定義:
urllib.request.urlretrieve(url,filename=None,reporthook=None, data=None)
利用urlretrieve() 將數據下載到本地。
- 參數 finename 指定了保存本地路徑(如果參數未指定,urllib會生成一個臨時文件保存數據。)
- 參數 reporthook 是一個回調函數,當連接上服務器、以及相應的數據塊傳輸完畢時會觸發該回調,我們可以利用這個回調函數來顯示當前的下載進度。
- 參數 data 指 post 到服務器的數據,該方法返回一個包含兩個元素的(filename, headers)元組,filename 表示保存到本地的路徑,header 表示服務器的響應頭。
用法:
>>> import urllib.request >>>local_filename,headers=urllib.request.urlretrieve('http://python.org/') >>> html = open(local_filename) >>> html.close()
注意:當html=open(local_filename),然后lines=html.readlines()時可能會出現unicode錯誤
處理方法:html=open(local_filename,'utf-8')這樣就會解決unicode問題。
例子:抓取web頁面
#coding:utf-8 from urllib.request import urlretrieve def firstNonBlank(lines): for eachLine in lines: if not eachLine.strip(): continue else: return eachLine def firstLast(webpage): f=open(webpage,encoding='utf-8') lines=f.readlines() f.close() print(firstNonBlank(lines)) lines.reverse() print(firstNonBlank(lines)) def download(url='http://www.baidu.com',process=firstLast): try: retval=urlretrieve(url)[0] except IOError: retval=None if retval: process(retval) if __name__=="__main__": download()
