zoukankan      html  css  js  c++  java
  • python解析ua

    一个非常神奇的包可以帮助我们优雅的解析浏览器的UA,他的名字叫做user_agents

    pip install pyyaml ua-parser user-agents

    >>>ua_string='Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B179 Safari/7534.48.3'
    >>># 浏览器属性
    >>>user_agent=parse(ua_string)
    >>>user_agent.browser
    Browser(family=u'Mobile Safari',version=(5,1),version_string='5.1')
    >>>user_agent.browser.family
    u'Mobile Safari'
    >>>user_agent.browser.version
    (5,1)
    >>>user_agent.browser.version_string
    '5.1'
    >>># 操作系统属性
    >>>user_agent.os
    OperatingSystem(family=u'iOS',version=(5,1),version_string='5.1')
    >>>user_agent.os.family
    u'iOS'
    >>>user_agent.os.version
    (5,1)
    >>>user_agent.os.version_string
    '5.1'
    >>># 设备属性
    >>>user_agent.device
    Device(family=u'iPhone',brand=u'Apple',model=u'iPhone')
    >>>user_agent.device.family
    u'iPhone'
    >>>user_agent.device.brand
    u'Apple'
    >>>user_agent.device.model
    u'iPhone'
    >>>
    >>>str(user_agent)
    'iPhone / iOS 5.1 / Mobile Safari 5.1'

    批量解析:采用apply函数

    • data['user_agent'] = data['ua'].apply(lambda x:parse(x))
  • 相关阅读:
    从书上学的东西(顺带总结一发)
    网上讲的好的知识点汇总
    土地征用题解(兼斜率优化详解)
    Blocks题解(区间dp)
    高精度模板汇总
    动态规划总结
    异或序列(题解)(莫队)
    小Z的袜子(题解)(莫队)
    小B的询问(题解)(莫队)
    凸包模板——Graham扫描法
  • 原文地址:https://www.cnblogs.com/xiao-xue-di/p/12216997.html
Copyright © 2011-2022 走看看