python下载图片如何保证质量最高_Python 下载图片的几种方法

总结下:

url = 'http://img.pconline.com.cn/images/upload/upc/tx/photoblog/1707/31/c14/54293429_1501509923353_mthumb.jpg'

1、urllib库——urlretrieve

importurllibdefreport_hook(count, block_size, total_size):print '%02d%%'%(100.0 * count * block_size/total_size)

urllib.urlretrieve("http://img.pconline.com.cn/images/upload/upc/tx/photoblog/1707/31/c14/54293429_1501509923353_mthumb.jpg",r'D:\DESKTOP\1.jpg',reporthook= report_hook)

顺便提一下,report_hook是回调函数——reporthook:是一个回调函数,当连接上服务器、以及相应的数据块传输完毕的时候会触发该回调。我们可以利用这个回调函数来显示当前的下载进度。

2、还是urllib——urlopen

importurllib

r=urllib.urlopen(url)

data=r.read()

with open("1234.jpg",'wb') as f:

f.write(data)

3、requests

#coding:utf-8

importrequests

r=requests.get(url)

with open("123.jpg",'wb') as f:

f.write(r.content)

注意:

resp.text返回的是Unicode型的数据。

resp.content返回的是bytes型也就是二进制的数据。

urlencode的发送请求同时传data表单

importurllibimporturllib2

url= 'http://www.someserver.com/register.cgi'values= {'name' : 'WHY','location' : 'SDU','language' : 'Python'}

data= urllib.urlencode(values) #编码工作

req = urllib2.Request(url, data) #发送请求同时传data表单

response = urllib2.urlopen(req) #接受反馈的信息

the_page = response.read() #读取反馈的内容

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值