Code to Parse User Agent String

Code to parse user agent string?

The get_browser() function has been available in PHP for quite a long a time.

The PHP manual is free, can be downloaded in various formats and viewed online (with comments)

How to parse user agent string using Python

There is a library called httpagentparser for that:

import httpagentparser
>>> s = "Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/532.9 (KHTML, like Gecko) Chrome/5.0.307.11 Safari/532.9"
>>> print httpagentparser.simple_detect(s)
('Linux', 'Chrome 5.0.307.11')
>>> print httpagentparser.detect(s)
{'os': {'name': 'Linux'},
'browser': {'version': '5.0.307.11', 'name': 'Chrome'}}

Parsing HTTP User-Agent string

I finally decided to write my own, and I am happy with the outcome. Please feel free to use/modify/send me patches, etc.

It's here: http://pypi.python.org/pypi/httpagentparser

Usage example:

>>> import httpagentparser
>>> s = "Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/532.9 (KHTML, like Gecko) \
Chrome/5.0.307.11 Safari/532.9"
>>> print(httpagentparser.simple_detect(s))
('Linux', 'Chrome 5.0.307.11')
>>> print(httpagentparser.detect(s))
{'os': {'name': 'Linux'},
'browser': {'version': '5.0.307.11', 'name': 'Chrome'}}

>>> s = "Mozilla/5.0 (Linux; U; Android 2.3.5; en-in; HTC_DesireS_S510e Build/GRJ90) \
AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1"
>>> print(httpagentparser.simple_detect(s))
('Android Linux 2.3.5', 'Safari 4.0')
>>> print(httpagentparser.detect(s))
{'dist': {'version': '2.3.5', 'name': 'Android'},
'os': {'name': 'Linux'},
'browser': {'version': '4.0', 'name': 'Safari'}}

Is there a good method for parsing the user-agent string?

For Java, take a look at User-Agent-Utils. It's fairly compact (< 50kB) and has no dependencies.

Note although the latest release is quite recent (1.21, released 2018-01-24), the library's page states:

Warning: This project is end-of-life and will not be updated regularly any longer

And on the github page it says:

EOL WARNING

This library has reached end-of-life and will not see regular updates
any longer.

Version 1.21 was the last official release in 2018.

Parsing Browser and OS info from the UserAgent String

Microsoft doesn't seem to recommend it: https://blogs.msdn.microsoft.com/ieinternals/2013/09/21/internet-explorer-11s-many-user-agent-strings/

There is a JS library that does this on the client side; it even demos itself on its site: http://faisalman.github.io/ua-parser-js/.

It seems that adding a particular user agent recognition is also easy if you have specific needs beyond what's offered.

Parsing mobile phone user agent string , good solution or library?

First, I would say KISS (Keep It Simple, Stupid) which is a widely used expression for a reason. I would start off by double checking my business needs and see how much device information I really need and what I am going to use it for. Maybe you only need to see which rendering engine the client is using?

Second, you should consider the parsing time. If you end up using i.e. WURFL and parsing that XML on your site, you would need to parse 400 000+ lines of XML when checking for device information. Of course, you could put the XML into a local indexed database, but that would also require some maintenance scripts to synchronize the data in the updated XML with the database.

Third (but maybe it should be first?) is considering the cost/benefit of the solution. If you make money on the site, it might be smart to leave some responsibility on a partner. A hosting service like handsetdetection.com seems capable of delivering high traffic at a not-so-terrifying cost. Another benefit is that they are responsible for maintaining their own repository and could lose customers if their service isn't good enough. The OpenSource community could theoretically go on a 4 month vacation and the soulution wouldn't be maintained in that period (I really wouldn't think that should be anything to worry about ;-)

Not knowing your exact needs, I would prioritize it like this:

  1. Use as simple a solution as possible, i.e. the solution from Detect Mobile Browsers
  2. Go OpenSource, like WURFL. I just love OpenSource solutions :-)
  3. If your business needs guaranteed stability and data quality, you should let the professionals handle it ;-)

Parsing User Agent in R but how to do with python?

This gets you all software with version and additional info if provided:

import re

string = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36"

software = re.findall(r"\w+/[\d\.]+\.? \(.+?\)|\w+/[\d\.]+\.?", string)

info = {}
for i in software:
version=re.search(r"(?<=/)[\d+\.]+\,?", i).group()
try:
additional_info = re.search(r"(\(.+?\))", i).group()
except:
additional_info = ""
info[re.search(r"\w+(?=/)",i).group()] = {"version":version, "additional_info":additional_info}

The first regex finds all the software and then I create a dictionary with the structure {"software1" : {"version" : version, "additional_info" : additional_info}, "software2" :....}

How to Parse UserAgent in Javascript

You can do something like:

if(/Windows NT 5\.1/.test(navigator.userAgent)){
// do code
}


Related Topics



Leave a reply



Submit