garbanzio
garbanzio

Reputation: 846

How do I detect a ipad/iphone webview in user agent using regex in Python?

All I need to do is to convert the following Javascript regex test to Python:

var is_ios_app = /(iPhone|iPod|iPad).*AppleWebKit(?!.*Safari)/i.test(navigator.userAgent);

Upvotes: 0

Views: 1663

Answers (2)

Mehmet Ince
Mehmet Ince

Reputation: 1318

There is python library which can use for that purpose.

https://pypi.python.org/pypi/httpagentparser

Example Usage:

>>> 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.detect(s)
{'dist': {'version': '2.3.5', 'name': 'Android'},
'os': {'name': 'Linux'},
'browser': {'version': '4.0', 'name': 'Safari'}}

So you can use name element of dist to get ios users.

Upvotes: 1

Sabuj Hassan
Sabuj Hassan

Reputation: 39365

Use re.search() for this in Python.

userAgent = "YOUR_UA"
pattern = '(iPhone|iPod|iPad).*AppleWebKit(?!.*Safari)'
if(re.search(pattern, userAgent)):
    print "true"

Upvotes: 0

Related Questions