浏览器请求头
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36
1、Flask 自带的请求头解析
from flask import request print(request.user_agent.platform) print(request.user_agent.version) print(request.user_agent.browser) print(request.user_agent.language) """ 解析结果 macos 88.0.4324.150 chrome None """
2、使用user-agents库
安装
pip install pyyaml ua-parser user-agents
示例
from user_agents import parse user_agent = parse(request.user_agent.string) print(user_agent.browser) print(user_agent.os) """ 解析结果: Browser(family='Chrome', version=(88, 0, 4324), version_string='88.0.4324') OperatingSystem(family='Mac OS X', version=(10, 14, 4), version_string='10.14.4') """