有没有办法检测用户代理是来自浏览器还是应用程序?
问题描述:
我需要区分来自HTTP标头的User-Agent字符串是来自移动(iOS和Android)浏览器还是应用程序。是否有任何工具/库,最好是用Python来帮助我?
I need to differentiate whether a User-Agent string from an HTTP header is from a mobile (iOS & Android) browser or a app. Is there any tool/library, preferably in Python which can help me with that?
答
user-agents 是一个相当简单的Python包,解析 User-Agent
字符串。
user-agents is a fairly simple package for Python, which parses the User-Agent
string.
from user_agents import parse
user_agent = parse(user_agent_string)
user_agent.os.family # This will get you what you need
因此,您可以拥有OS系列判断请求是来自桌面浏览器还是移动浏览器或应用客户端。
So having the OS family you can judge whether the request is made from a desktop browser or mobile browser or app client.