Beatifulsoup: how to get image size by url

ε祈祈猫儿з 提交于 2020-03-04 17:39:52

问题


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"/>

回答1:


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


来源:https://stackoverflow.com/questions/51116907/beatifulsoup-how-to-get-image-size-by-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!