LoginSignup
5
8

More than 5 years have passed since last update.

PythonでUser Agentをパーズする

Posted at

User Agentをパーズするために、httpagentparserを使ってみました。
手っ取り早い方法ですと、こんな感じで使えます。

import httpagentparser
 
user_agent = 'Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/532.9 (KHTML, like Gecko) Chrome/5.0.307.11 Safari/532.9'
httpagentparser.simple_detect(user_agent)
# ('Linux', 'Chrome 5.0.307.11')

httpagentparser.detect()の場合、User Agentの情報をもう少し細かい単位で取ることができます。

httpagentparser.detect(user_agent)
# {'platform': {'version': None, 'name': 'Linux'}, 'os': {'name': 'Linux'}, 'bot': False, 'browser': {'version': '5.0.307.11', 'name': 'Chrome'}}

Python 3にも対応しております。User Agentに応じた処理を書く必要が生じた場合には、便利かもしれません。

5
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
8