python 解析User-Agent

需求:从User-Agent中获取浏览器信息和操作系统信息

使用第三方库ua-parser

安装

pip install ua-parser

使用

from ua_parser.user_agent_parser import Parse

# 解析user_agent
parsed_user_agent = Parse(user_agent)

print(parsed_user_agent)

返回数据

{"string": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36", "user_agent": {"family": "Chrome", "major": "118", "minor": "0", "patch": "0"}, "os": {"family": "Windows", "major": "10", "minor": null, "patch": null, "patch_minor": null}, "device": {"family": "Other", "brand": null, "model": null}}

正常解析就可以了

posted @ 2023-10-24 14:22  yingzi__block  阅读(176)  评论(0编辑  收藏  举报