Search code examples
pythonbrowseruser-agent

python-user-agents library is not working


I am trying to use the python-user-agents. I keep running into a number of bugs within the library itself.

First it referred to a from ua_parser import user_agent_parser that it never defined. So after banging my head, I looked online to see what that might be and found that ua_parser is yet another library that this project was using. So I downloaded ua_parser. But now I am getting an error that

TypeError: parse_device() got an unexpected keyword argument 'model'

Sure enough, ua_parser has a model variable that the python-user-agents library is not expecting. Has anyone done a better job with this library? Whoever wrote it clearly did a terrible job. But it seems to be the only thing out there that I could find. Any help fixing it to work well? I am looking to use it so to identify if a browser's device is mobile or touchable or a tablet as in: user_agent.is_mobile or user_agent.is_touch_capable or user_agent.is_tablet


Solution

  • if you look at the readme from the github link it tells you what to install and how to use the lib:

    You need pyyaml and ua-parser:

    pip install pyyaml ua-parser user-agents
    

    A working example:

    In [1]: from user_agents import parse
    
    In [2]: 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'
    
    In [3]: user_agent = parse(ua_string)
    
    In [4]: user_agent.is_mobile
    Out[4]: True
    
    In [5]: user_agent.is_touch_capable 
    Out[5]: True
    
    In [6]: user_agent.is_tablet
    Out[6]: False