python下载图片 referer_python请求服务器图片并下载到本地磁盘

>>> importos>>>os.getcwd()'C:\\Python33'

>>> os.chdir('E:\\python\\mmy')>>>os.getcwd()'E:\\python\\mmy'

>>> importurllib.request>>> urllib.request.urlopen('http://image.edai.com/avatar/000/88/14/23_avatar_middle.jpg')

>>> 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()

>>> print(response.info())

Expires: Thu,31 Dec 2037 23:55:55GMT

Date: Sat,30 Jan 2016 13:18:38GMT

Server: nginx/0.8.42Content-Type: image/jpeg

Content-Length: 8053Last-Modified: Thu, 08 Jan 2015 06:46:11GMT

Pragma: public

Accept-Ranges: bytes

Age:1X-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

>>>

843598-20160130214251083-2115983975.png

在本地磁盘已经有了liuhui.jpg了!!!

以上代码是在idle下操作的,其实真实的代码只有如下:

importurllib.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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值