python获取网络图片方法及整理过程详解
Python  /  管理员 发布于 7年前   288
这篇文章主要介绍了python获取网络图片方法及整理过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
方式1
使用urllib库
import urllib.requestimport os ,staturl = "https://cn.bing.com/th?id=OHR.Lidong2019_ZH-CN0761273672_1920x1080.jpg"try: urllib.request.urlretrieve(url,filename="/home/baixiaoxu/desk/123.jpg")except IOError as e: print("IOE ERROR")except Exception as e: print("Exception")注意:1,获取地址,判断地址是否存在2,本地保存地址,判断存在3,获取远程地址的图片名,或改名"""url = "https://cn.bing.com/th?id=OHR.Lidong2019_ZH-CN0761273672_1920x1080.jpg"file_suffix = os.path.split(url)[1][-20:-1]print(file_suffix)"""
2,使用系统库文件读写操作
import urllib.requestimport os ,statreq = urllib.request.Request(url)file = "/home/baixiaoxu/desk/file-ttttt.jpg"req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.3; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0')response = urllib.request.urlopen(url)html = response.read()with open(file, 'wb') as f: f.write(html)
网上的方法
import osos.makedirs('./image/', exist_ok=True)IMAGE_URL = "http://image.nationalgeographic.com.cn/2017/1122/20171122113404332.jpg" def urllib_download(): from urllib.request import urlretrieve urlretrieve(IMAGE_URL, './image/img1.png') def request_download(): import requests r = requests.get(IMAGE_URL) with open('./image/img2.png', 'wb') as f: f.write(r.content)def chunk_download(): import requests r = requests.get(IMAGE_URL, stream=True) with open('./image/img3.png', 'wb') as f: for chunk in r.iter_content(chunk_size=32): f.write(chunk)
整理简单的下载图片
import urllibfrom urllib import requestimport reresponse = request.urlopen('https://cn.bing.com/')html = response.read()ht = html.decode()pattern = r'bgLink(.*?\.jpg)'compile_re = re.compile(pattern)hh = compile_re.findall(ht)url = hh[0].split('/')[1]download = 'https://cn.bing.com/' + urlurllib.request.urlretrieve(download,filename="/home/baixiaoxu/desk/download.jpg")
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号