Beatifulsoup:如何通过URL获取图像大小 [英] Beatifulsoup: how to get image size by url

查看:94
本文介绍了Beatifulsoup:如何通过URL获取图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果提取的图像网址是我使用的是get('width'),则我需要获取宽度和高度,但这似乎不起作用

I need to get width and height if the extracted image url, i used get('width'), but this seems not working

description = soup.find("div", id="module_product_detail")
img= description.find("img")
print(img.get('width'))

输出为无. 链接看起来像这样

The output is none. link looks like this

<img alt="image" src="https://bos1.lightake.net:20011/UploadFiles/ShopSkus/1000x1000/Y2463/Y246302/sku_Y246302_1.jpg"/>

推荐答案

由于没有width或height属性,因此访问图像的METADATA的唯一方法是下载图像并像这样读取它:

Since there's not width nor height attribute, the only way to access the METADATA of the image is by downloading it and reading it like this:

from requests import get
from io import BytesIO
from PIL import Image


image_raw = get('https://upload.wikimedia.org/wikipedia/commons/3/30/Googlelogo.png')
image = Image.open(BytesIO(image_raw.content))
width, height = image.size

print(width, height)

# Output:
# 1368 469

这篇关于Beatifulsoup:如何通过URL获取图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆