Skip to main content
 首页 » 编程设计

python之在 Python Bottle 中获取用户浏览器信息

2025年05月04日12yxwkf

我试图找出我的用户使用的是哪些浏览器,但我遇到了问题。 如果我尝试阅读标题“User-Agent”,它通常会给我很多文本,但什么也没有告诉我。 例如,如果我使用 Chrome 访问该网站,在“User-Agent” header 中有:

用户代理:“Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36”。

如您所见,这没有告诉我任何信息,因为提到了 Mozzila、Safari、Chrome 等。即使我访问过 Chrome。

我一直在使用的框架是 Bottle (Python)。

如有任何帮助,我们将不胜感激。

请您参考如下方法:

User-Agent: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36".

As you can see, this tells me nothing since there is mention of Mozzila, Safari, Chrome etc.. even though I visited with Chrome.

你上面的结论是错误的。 UA 会告诉您很多信息,包括网络浏览器的类型和版本。

下面的帖子解释了为什么 Mozilla 和 Safari 存在于 Chrome 的 UA 中。

History of the browser user-agent string

您可以尝试在user-agent-string-db 上手动分析它.

有一个 Python API

from uasparser2 import UASparser 
 
uas_parser = UASparser() 
# Instead of fecthing data via network every time, you can cache the db in local 
# uas_parser = UASparser('/path/to/your/cache/folder', mem_cache_size=1000) 
# Updating data is simple: uas_parser.updateData() 
 
 
result = ua_parser.parse('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36') 
 
 
# result 
{'os_company': u'', 
 'os_company_url': u'', 
 'os_family': u'Linux', 
 'os_icon': u'linux.png', 
 'os_name': u'Linux', 
 'os_url': u'http://en.wikipedia.org/wiki/Linux', 
 'typ': u'Browser', 
 'ua_company': u'Google Inc.', 
 'ua_company_url': u'http://www.google.com/', 
 'ua_family': u'Chrome', 
 'ua_icon': u'chrome.png', 
 'ua_info_url': u'http://user-agent-string.info/list-of-ua/browser-detail?browser=Chrome', 
 'ua_name': u'Chrome 31.0.1650.57', 
 'ua_url': u'http://www.google.com/chrome'}