python获取网络图片方法及整理过程详解
作者:Lust4Life 时间:2022-10-21 19:33:20
这篇文章主要介绍了python获取网络图片方法及整理过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
方式1
使用urllib库
import urllib.request
import os ,stat
url = "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.request
import os ,stat
req = 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 os
os.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 urllib
from urllib import request
import re
response = 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/' + url
urllib.request.urlretrieve(download,filename="/home/baixiaoxu/desk/download.jpg")
来源:https://www.cnblogs.com/g2thend/p/11818599.html
标签:python,获取,网络,图片,方法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
opencv实现图像旋转效果
2023-07-17 13:28:40
![](https://img.aspxhome.com/file/2023/4/61444_0s.jpg)
PHP实现加减乘除最简单的实例分享
2023-06-14 16:17:41
Python关于__name__属性的含义和作用详解
2021-10-28 09:29:51
![](https://img.aspxhome.com/file/2023/2/77592_0s.png)
python 爬取疫情数据的源码
2022-05-22 13:21:54
![](https://img.aspxhome.com/file/2023/1/66831_0s.jpg)
选择utf-8还是GB2312?
2009-06-19 13:05:00
![](https://img.aspxhome.com/file/UploadPic/20096/19/11e5d33a4f2g215-39s.jpg)
视觉设计常见误解
2008-11-13 13:09:00
![](https://img.aspxhome.com/file/UploadPic/200811/13/fig1-address-91s.gif)
深刻理解Oracle数据库的启动和关闭
2010-07-26 13:08:00
OpenCV+Python几何变换的实现示例
2022-02-09 14:44:52
![](https://img.aspxhome.com/file/2023/7/95747_0s.jpg)
python去除列表中的空值元素实战技巧
2023-12-08 12:16:06
python3 requests库实现多图片爬取教程
2023-11-22 15:37:40
![](https://img.aspxhome.com/file/2023/0/105790_0s.jpg)
如何使用python获取现在的日期与时间
2021-07-21 16:50:12
![](https://img.aspxhome.com/file/2023/7/120567_0s.jpg)
JavaScript 获取事件对象的一个注意点
2009-07-24 11:49:00
用Python做的数学四则运算_算术口算练习程序(后添加减乘除)
2023-04-10 13:27:35
Flask框架web开发之零基础入门
2021-01-03 22:23:02
![](https://img.aspxhome.com/file/2023/4/83944_0s.png)
PHP实现的MD5结合RSA签名算法实例
2023-11-08 05:03:00
基于tkinter中ttk控件的width-height设置方式
2023-07-04 21:32:20
![](https://img.aspxhome.com/file/2023/3/62563_0s.png)
python解析中国天气网的天气数据
2023-01-20 18:48:39
Python常用的文件及文件路径、目录操作方法汇总介绍
2022-03-21 00:00:25
《悟透JavaScript》之 甘露模型
2008-06-09 14:03:00
![](https://img.aspxhome.com/file/UploadPic/20086/9/200869142355966s.gif)
pandas-resample按时间聚合实例
2023-05-26 19:07:50
![](https://img.aspxhome.com/file/2023/5/101085_0s.jpg)