原文:https://blog.csdn.net/follow_sunshine/article/details/84994114
安装工具库
pip install pyyaml ua-parser user-agents
示例
from user_agents import parse
ua_string = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36'
user_agent = parse(ua_string) # 解析成user_agent
bw = user_agent.browser.family # 判断是什么浏览器
s = user_agent.os.family # 判断是什么操作系统
juge_pc = user_agent.is_pc # 判断是不是桌面系统
phone = user_agent.device.family
print(bw,s,phone,juge_pc)
out: Chrome Windows Other True
其他属性判断:
is_mobile:判断是不是手机
is_tablet:判断是不是平板
is_pc:判断是不是桌面系统
is_touch_capable:有没有触屏功能
is_bot:是不是搜索引擎的爬虫