python 对user_agent进行 解析
最近对公司网站的日志数据进行数据分析的时候需要对用户的user_agent进行处理,于是从网上大概了解了关于python解析user_agent的方法,并作了简单记录
安装工具库
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:是不是搜索引擎的爬虫
常见机型映射字典
map_phone = {'Apple': 'Apple', 'KIW-AL10': 'Huawei','PRA-TL10': 'Huawei', 'BND-AL00': 'Huawei', 'XiaoMi': 'XiaoMi',
'MIX 2': 'XiaoMi','Oppo': 'Oppo',' Oppo': 'Oppo','Gionee': 'Gionee', 'Samsung': 'Samsung', 'PRA-AL00X': 'Huawei',
'PACM00': 'Oppo', 'PBET00': 'Oppo', 'R7Plusm': 'Oppo', 'PAAT00': 'Oppo', 'PBAM00': 'Oppo', 'PADM00': 'Oppo', 'PAFM00': 'Oppo','PBEM00': 'Oppo', 'PAAM00': 'Oppo', 'PBBM00': 'Oppo', 'PACT00': 'Oppo', 'V1809A': 'vivo', 'PBAT00': 'Oppo', 'PADT00': 'Oppo', 'BND-TL10': 'Huawei', 'PBBT00':' Oppo', 'PBCM10':'Oppo','Mi Note
3':'XiaoMi','V1816A':'vivo','V1732T':'vivo','V1813A':'vivo','V1732A':'vivo','V1818A':'vivo','CAM-TL00':'Huawei','Le X620':'leshi','M6 Note':'meizu','m3 note':'meizu','M5':'meizu','M1 E ':'meizu','BLN-AL10':'Huawei','M5 Note':'meizu','PRA-AL00':'honour','LND-AL30':'honour','NEM-AL10':'honour','BND-AL10':'honour','CAM-AL00':'honour','SCL-TL00':'honour','LLD-AL30':'honour','BLN-AL20':'honour','AUM-AL20':'honour','JSN-AL00':'honour','LLD-AL10':'honour','BLN-TL10':'honour',
'LLD-AL20':'honour','BLN-AL40':'honour','MYA-AL10':'honour','LLD-AL00':'honour','JSN-AL00a':'honour','JMM-AL10':'honour','DLI-AL10':'honour','JMM-AL00':'honour','V1809T':'vivo','LND-AL40':'honour','PLK-AL10':'honour','MX6':'meizu','PLK-TL01H':'honour','S9':'Samsung','KIW-TL00':'honour','V1813T':'vivo'}