>>> import os >>> os.getcwd() 'C:\\Python33' >>> os.chdir('E:\\python\\mmy') >>> os.getcwd() 'E:\\python\\mmy' >>> import urllib.request >>> urllib.request.urlopen('http://image.edai.com/avatar/000/88/14/23_avatar_middle.jpg') <http.client.HTTPResponse object at 0x00000000032E0FD0> >>> response = urllib.request.urlopen('http://image.edai.com/avatar/000/88/14/23_avatar_middle.jpg') >>> response.getcode() 200 >>> response.geturl() 'http://image.edai.com/avatar/000/88/14/23_avatar_middle.jpg' >>> response.info() <http.client.HTTPMessage object at 0x00000000032ED6A0> >>> print(response.info()) Expires: Thu, 31 Dec 2037 23:55:55 GMT Date: Sat, 30 Jan 2016 13:18:38 GMT Server: nginx/0.8.42 Content-Type: image/jpeg Content-Length: 8053 Last-Modified: Thu, 08 Jan 2015 06:46:11 GMT Pragma: public Accept-Ranges: bytes Age: 1 X-Via: 1.1 scxx84:1 (Cdn Cache Server V2.0) Connection: close Cache-Control: public, must-revalidate, proxy-revalidate >>> pic = response.read() >>> with open('liuhui.jpg', 'wb') as f: f.write(pic) 8053 >>>

在本地磁盤已經有了liuhui.jpg了!!!
以上代碼是在idle下操作的,其實真實的代碼只有如下:
import urllib.request response = urllib.request.urlopen('http://image.edai.com/avatar/000/88/14/23_avatar_middle.jpg') pic = response.read() with open('liuhui.jpg', 'wb') as f: f.write(pic)
注:
1.urlopen()的參數可以是一個字符串或一個request對象,當為一個字符串時,其實是執行了兩個步驟:
(1)req = urllib.request.Request('http://image.edai.com/avatar/000/88/14/23_avatar_middle.jpg');
(2)response = urllib.request.urlopen(req).
2.urllib.request.urlopen()的返回值是一個http.client.HTTPResponse對象,即客戶端http響應結果對象。我們知道http協議,對客戶端請求是需要響應的,而響應的數據包還包含了http頭部信息。因此,urllib.request.urlopen()返回的對象還有以下3個重要的方法:
(1)getcode(): Htpp狀態碼
(2)geturl():客戶端請求的url地址
(3)info():就是http的頭部信息(header)
