You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python 3.8中将favicon库返回的Icon对象转为字典

批量将favicon库的Icon对象转为属性字典列表

方法一:手动提取指定属性(推荐,可控性强)

先获取目标网站的图标列表,再遍历每个Icon对象,手动提取需要的属性构建字典:

import favicon

# 获取目标网站的图标列表
icons = favicon.get("https://example.com")

# 批量转换为属性字典列表
icon_dicts = [
    {
        "url": icon.url,
        "width": icon.width,
        "height": icon.height,
        "format": icon.format
    } for icon in icons
]

方法二:利用vars()快速转换(适合确认对象无多余属性的场景)

如果确定Icon对象仅包含url、width、height、format四个属性,可以用vars()直接获取对象的属性字典,再批量处理:

import favicon

icons = favicon.get("https://example.com")

# 直接批量转换
icon_dicts = [vars(icon) for icon in icons]

# 若需过滤多余属性,可改为:
# icon_dicts = [{k: vars(icon)[k] for k in ["url", "width", "height", "format"]} for icon in icons]

转换完成后,icon_dicts就是包含所有图标属性键值对的字典列表,每个元素对应一个图标的完整属性信息。

内容的提问来源于stack exchange,提问作者Ananth

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 00:03:14