python获取网页中所有图片并筛选指定分辨率的方法
作者:YY_姚 时间:2021-07-28 04:57:23
压测时,图片太少,想着下载网页中的图片,然后过滤指定分辨率,但网页中指定分辨率的图片太少了(见下)
后使用格式工厂转换图片
import urllib.request # 导入urllib模块
import re # 导入re模块
import os
from PIL import Image
htmlurl = 'http://www.win4000.com/wallpaper_detail_134824_3.html'
downloadpath = 'C:\\Users\\yaowanjun\\Desktop\\img\\'
def getHTML(htmlurl):
req = urllib.request.urlopen(htmlurl)
buf = req.read()
return buf.decode('utf-8')
def downloadImg(buf):
req = r'src="(.+?\.jpg)"' #正则表达式,匹配图片格式
imgreq = re.compile(req) #编译正则表达式
imglist = re.findall(imgreq, buf)
# print(imglist)
x = 0
if not os.path.isdir(downloadpath):#若没有则创建
os.makedirs(downloadpath)
paths = downloadpath
for imgurl in imglist:
f = open(paths + str(x) +'.jpg',"wb") #打开文件
req = urllib.request.urlopen(imgurl)
buf = req.read() #读出文件
f.write(buf)
f.close()
x = x + 1
return imglist
def saveImg():
for filenumber in os.walk(downloadpath):
# print(filenumber[2])
for files in filenumber[2]:
# print(files)
singleimg = Image.open(downloadpath + files)
singleimg.close()
#print(singleimg.size, singleimg.width, singleimg.height)
if singleimg.size == (1920, 1080):
print(singleimg)
else:
os.remove(downloadpath + files)
buf = getHTML(htmlurl)
downloadImg(buf)
saveImg()
执行结果:
指定网页所有图片
不符合指定分辨率的图片删除后
来源:https://blog.csdn.net/weixin_38208401/article/details/78324469
标签:python,图片,分辨率
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python中隐藏的五种实用技巧分享
2023-08-23 15:12:05
参数传递解决window.open的session变量丢失
2007-10-22 17:40:00
Go语言实现AOI区域视野管理流程详解
2023-06-20 03:21:51
![](https://img.aspxhome.com/file/2023/7/90477_0s.png)
关于多元线性回归分析——Python&SPSS
2023-03-11 17:03:34
![](https://img.aspxhome.com/file/2023/0/67230_0s.jpg)
流动的线条 —— 中国汉字书法之美
2009-10-30 18:15:00
![](https://img.aspxhome.com/file/UploadPic/200910/30/1-79s.jpg)
不是原型继承那么简单!prototype的深度探索
2008-03-07 12:42:00
条件CSS的使用[译]
2009-03-12 12:23:00
![](https://img.aspxhome.com/file/UploadPic/20093/12/01-51s.jpg)
Oracle备库宕机启动的完美解决方案
2023-07-20 05:28:34
![](https://img.aspxhome.com/file/2023/9/64079_0s.png)
js表格拖选动态效果COOL而实用
2007-08-05 12:07:00
![](https://img.aspxhome.com/file/UploadPic/up/2007080512171065.gif)
ORACLE 如何查询被锁定表及如何解锁释放session
2023-07-02 11:59:39
javascript基础之数组(Array)对象
2008-06-25 13:32:00
对SQL Server聚集索引的指示综合描述
2010-08-31 14:25:00
一个简单的鼠标划过切换效果js源码
2010-06-21 10:55:00
![](https://img.aspxhome.com/file/UploadPic/20106/21/100523-64s.jpg)
ASP使用FSO组件生成HTML静态页面
2007-10-15 12:19:00
Div的浮动+循环(描述的不清楚,请看图)
2008-09-22 20:21:00
![](https://img.aspxhome.com/file/UploadPic/20089/22/banner_85s.jpg)
防盗链接ASP函数
2011-03-07 11:02:00
sqlserver 巧妙的自关联运用
2012-07-21 14:55:12
Python机器学习NLP自然语言处理基本操作关键词
2023-11-09 05:51:31
![](https://img.aspxhome.com/file/2023/9/63549_0s.gif)
Oracle 触发器的使用小结
2009-05-24 19:54:00
FrontPage XP设计教程1——站点初建与管理
2008-10-11 12:13:00
![](https://img.aspxhome.com/file/UploadPic/200810/17/20081017121637930.jpg)