问题
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