user_agent library documentation¶
The user_agent is a library for:
- generating extended user agent config that could be used in JS environment
- generating simple user-agent string that could be used as content of User-Agent HTTP header
Supported platforms: windows, linux, mac.
Supported browsers: chrome, firefox, ie.
-
user_agent.
generate_user_agent
(os=None, navigator=None, platform=None, device_type=None)[source]¶ Generates HTTP User-Agent header
Parameters: - os (string or list/tuple or None) – limit list of os for generation
- navigator (string or list/tuple or None) – limit list of browser engines for generation
- device_type (list/tuple or None, possible values: "desktop", "smartphone", "tablet", "all") – limit possible oses by device type
Returns: User-Agent string
Return type: string
Raises: - InvalidOption – if could not generate user-agent for any combination of allowed oses and navigators
- InvalidOption – if any of passed options is invalid
Returns a string like:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36
Generates web navigator’s config
Parameters: - os (string or list/tuple or None) – limit list of oses for generation
- navigator (string or list/tuple or None) – limit list of browser engines for generation
- device_type (list/tuple or None, possible values: "desktop", "smartphone", "tablet", "all") – limit possible oses by device type
Returns: User-Agent config
Return type: dict with keys (os, name, platform, oscpu, build_version, build_id, app_version, app_name, app_code_name, product, product_sub, vendor, vendor_sub, user_agent)
Raises: - InvalidOption – if could not generate user-agent for any combination of allowed platforms and navigators
- InvalidOption – if any of passed options is invalid
Returns a dict like:
{'app_code_name': 'Mozilla',
'app_name': 'Netscape',
'app_version': '5.0 (Windows)',
'build_id': '20170124100044',
'build_version': '51.0',
'navigator_id': 'firefox',
'os_id': 'win',
'oscpu': 'Windows NT 6.2',
'platform': 'Windows NT 6.2',
'product': 'Gecko',
'product_sub': '20100101',
'user_agent': 'Mozilla/5.0 (Windows NT 6.2; rv:51.0) Gecko/20100101 '
'Firefox/51.0',
'vendor': '',
'vendor_sub': ''}
Generates web navigator’s config with keys corresponding to keys of windows.navigator JavaScript object.
Parameters: - os (string or list/tuple or None) – limit list of oses for generation
- navigator (string or list/tuple or None) – limit list of browser engines for generation
- device_type (list/tuple or None, possible values: "desktop", "smartphone", "tablet", "all") – limit possible oses by device type
Returns: User-Agent config
Return type: dict with keys (TODO)
Raises: - InvalidOption – if could not generate user-agent for any combination of allowed oses and navigators
- InvalidOption – if any of passed options is invalid
Returns a dict like:
{'appCodeName': 'Mozilla',
'appName': 'Netscape',
'appVersion': '5.0 (Macintosh; Intel Mac OS X 10_8_1) AppleWebKit/537.36 '
'(KHTML, like Gecko) Chrome/54.0.2840.93 Safari/537.36',
'buildID': None,
'oscpu': 'Intel Mac OS X 10_8_1',
'platform': 'Macintosh; Intel Mac OS X 10_8_1',
'product': 'Gecko',
'productSub': '20030107',
'userAgent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_1) '
'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.93 '
'Safari/537.36',
'vendor': 'Google Inc.',
'vendorSub': ''}
External links¶
- Lists of user agents: