The primary way to parse a User-Agent string is using the detect function. It returns a dictionary containing structured information about the client's operating system, distribution, flavor, and browser.
By default, if a component (like a browser or OS) is not detected, it will be omitted from the dictionary. If you set fill_none=True, the resulting dictionary will include os and browser keys with name and version set to None if they weren't found.
Supported information types include:
os: Operating Systemdist: Distributionflavor: Flavorbrowser: Browserbot: A boolean indicating if the agent is a known bot.
import httpagentparser
agent_string = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
result = httpagentparser.detect(agent_string)
# Example result structure:
# {
# 'os': {'name': 'Windows', 'version': '10'},
# 'browser': {'name': 'Chrome', 'version': '91.0.4472.124'}
# }