admin管理员组

文章数量:1562427

User Agent中文名为用户代理,是Http协议中的一部分,属于头域的组成部分,User Agent也简称UA。它是一个特殊字符串头,是一种向访问网站提供你所使用的浏览器类型及版本、操作系统及版本、浏览器内核、等信息的标识。通过这个标识,用户所访问的网站可以显示不同的排版从而为用户提供更好的体验或者进行信息统计;例如用手机访问谷歌和电脑访问是不一样的,这些是谷歌根据访问者的UA来判断的。UA可以进行伪装。(wiki)


分析浏览器的User-Agent 我们可以收集客户端相关信息:是否手机、操作系统、浏览器等信息。

 

效果:http://1.billo.sinaapp/


以下为获取json格式ua的函数

ua为字符串格式的 User-Agent 字段


[python] view plain copy print ?
  1. import urllib  
  2. import urllib2  
  3. import json  
  4. def getUA(ua):  
  5.     ua = urllib.quote(ua)  
  6.     url = "http://www.useragentstring/?uas=%s&getJSON=all" %ua  
  7.     keys = {  
  8.             "Host":"www.useragentstring",  
  9.             "User-Agent":"Mozilla/5.0 (Windows NT 6.4; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0"  
  10.         }  
  11.     req = urllib2.Request(url)  
  12.     for key in keys:  
  13.         req.add_header(key,keys[key])  
  14.     j = urllib2.urlopen(req).read()  
  15.     j = json.loads(j)  
  16. return j  
  17.    
import urllib
import urllib2
import json
def getUA(ua):
    ua = urllib.quote(ua)
    url = "http://www.useragentstring/?uas=%s&getJSON=all" %ua
    keys = {
            "Host":"www.useragentstring",
            "User-Agent":"Mozilla/5.0 (Windows NT 6.4; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0"
        }
    req = urllib2.Request(url)
    for key in keys:
        req.add_header(key,keys[key])
    j = urllib2.urlopen(req).read()
    j = json.loads(j)
return j
 



request.headers['User-Agent'] 可以返回 User-Agent 的字符串

然后通过useragentstring这个网站的api可以获得一个解析后的User-Agent的json




识别是否为手机客户端的只要识别User-Agent中是否有"Mobile"字段即可

 

 

系统识别

如果是windows系统,解析User-Agent得到的json中的os_name是windows是内核版本 


目前主流的系统标识对应如下:

"Windows NT 6.4":"Windows 10",

"Windows NT 6.3":"Windows 8.1",

"Windows NT 6.2":"Windows 8",

"Windows NT 6.0":"Windows vista",

"Windows NT 6.1":"Windows 7",

"Windows NT 5.1":"Windows XP",

建立字典遍历这些标识便可

 



useragentstring返回的agent_name无法识别部分浏览器,只能显示内核


浏览器识别

IE浏览器判断的标准是”MSIE“字段

UC浏览器的判断标准是”UCWEB“字段”UCBrowser”字段

opera浏览器的判断标准是“opera“字段

搜狗浏览器的判断标准是”SE“”MetaSr“字段

搜狗手机浏览器的判断标准是SogouMobileBrowser"字段

chrome浏览器的判断标准是chrome字段

Safari浏览器的判断标准是safari字段

Firefox的判断标准是Firefox字段

QQ浏览器包含”TencentTraveler“或者”QQBrowser“字段

360浏览器的判断标准是”360SE”字段

Theworld浏览器的判断标准是”The world“字段

遨游浏览器的判断标准是”Maxthon“字段

 

 

下面建立一个字典存储主流浏览器类型:

Browser = {

        "SogouMobileBrowser":"搜狗手机浏览器",

        "UCBrowser":"UC浏览器",

        "UCWEB":"UC浏览器",

        "Opera":"Opera浏览器",

        "QQBrowser":"QQ浏览器",

        "TencentTraveler":"QQ浏览器",

        "MetaSr":"搜狗浏览器",

        "360SE":"360浏览器",

        "The world":"世界之窗浏览器",

        "Maxthon":"遨游浏览器",

        }

遍历这些标识便可

 

一个根据userAgent判断操作系统和浏览器类型的例子

[python] view plain copy print ?
  1. #coding:utf-8  
  2. import tornado.web  
  3. import sys  
  4. import urllib  
  5. import urllib2  
  6. import json  
  7. import os.path  
  8. from tornado.httpclient import AsyncHTTPClient  
  9.   
  10.   
  11.   
  12. class WelcomeHandler(tornado.web.RequestHandler):  
  13.     def get(self):  
  14.         ua = self.request.headers['User-Agent']  
  15.         isMobile = "你不是手机客户端" if ua.find("Mobile") == -1 else "你是手机客户端"  
  16.         uainfor = self.getUA(ua)  
  17.         self.render('index.html',  
  18.                     user=self.current_user,  
  19.                     head=self.request.headers['User-Agent'],  
  20.                     h=isMobile,  
  21.                     ua=uainfor  
  22.                     )  
  23.     def getUA(self, ua):  
  24.         ua_url = urllib.quote(ua) # 转url编码  
  25.         url = "http://www.useragentstring/?uas=%s&getJSON=all" % ua_url  
  26.         keys = {  
  27.                 "Host":"www.useragentstring",  
  28.                 "User-Agent":"Mozilla/5.0 (Windows NT 6.4; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0"  
  29.             }  
  30.         req = urllib2.Request(url)  
  31.         for key in keys:  
  32.             req.add_header(key,keys[key])  
  33.         j = urllib2.urlopen(req).read()  
  34.         j = json.loads(j)  
  35.         Windows = {  
  36.         "Windows NT 6.4":"Windows 10",  
  37.         "Windows NT 6.3":"Windows 8.1",  
  38.         "Windows NT 6.2":"Windows 8",  
  39.         "Windows NT 6.0":"Windows 8",  
  40.         "Windows NT 6.1":"Windows 7",  
  41.         "Windows NT 5.1":"Windows XP",  
  42.         }  
  43.         Browser = {  
  44.         "SogouMobileBrowser":"搜狗手机浏览器",  
  45.         "UCBrowser":"UC浏览器",  
  46.         "UCWEB":"UC浏览器",  
  47.         "Opera":"Opera浏览器",  
  48.         "QQBrowser":"QQ浏览器",  
  49.         "TencentTraveler":"QQ浏览器",  
  50.         "MetaSr":"搜狗浏览器",  
  51.         "360SE":"360浏览器",  
  52.         "The world":"世界之窗浏览器",  
  53.         "Maxthon":"遨游浏览器",  
  54.         }  
  55.         for name in Windows:  
  56.             if ua.find(name) != -1:  
  57.                 j["os_name"] = Windows[name]  
  58.         for name in Browser:  
  59.             if ua.find(name) != -1:  
  60.                 j["agent_name"] = Browser[name]  
  61.         return j  
  62.   
  63. settings = {  
  64.         "template_path": os.path.join(os.path.dirname(__file__), "templates"),  
  65.         "debug"True,  
  66. }  
  67.   
  68. # application should be an instance of `tornado.web.Application`,  
  69. # and don't wrap it with `sae.create_wsgi_app`  
  70. application = tornado.web.Application([  
  71.         (r'/', WelcomeHandler),  
  72.     ], **settings)  
#coding:utf-8
import tornado.web
import sys
import urllib
import urllib2
import json
import os.path
from tornado.httpclient import AsyncHTTPClient



class WelcomeHandler(tornado.web.RequestHandler):
    def get(self):
        ua = self.request.headers['User-Agent']
        isMobile = "你不是手机客户端" if ua.find("Mobile") == -1 else "你是手机客户端"
        uainfor = self.getUA(ua)
        self.render('index.html',
                    user=self.current_user,
                    head=self.request.headers['User-Agent'],
                    h=isMobile,
                    ua=uainfor
                    )
    def getUA(self, ua):
        ua_url = urllib.quote(ua) # 转url编码
        url = "http://www.useragentstring/?uas=%s&getJSON=all" % ua_url
        keys = {
                "Host":"www.useragentstring",
                "User-Agent":"Mozilla/5.0 (Windows NT 6.4; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0"
            }
        req = urllib2.Request(url)
        for key in keys:
            req.add_header(key,keys[key])
        j = urllib2.urlopen(req).read()
        j = json.loads(j)
        Windows = {
        "Windows NT 6.4":"Windows 10",
        "Windows NT 6.3":"Windows 8.1",
        "Windows NT 6.2":"Windows 8",
        "Windows NT 6.0":"Windows 8",
        "Windows NT 6.1":"Windows 7",
        "Windows NT 5.1":"Windows XP",
        }
        Browser = {
        "SogouMobileBrowser":"搜狗手机浏览器",
        "UCBrowser":"UC浏览器",
        "UCWEB":"UC浏览器",
        "Opera":"Opera浏览器",
        "QQBrowser":"QQ浏览器",
        "TencentTraveler":"QQ浏览器",
        "MetaSr":"搜狗浏览器",
        "360SE":"360浏览器",
        "The world":"世界之窗浏览器",
        "Maxthon":"遨游浏览器",
        }
        for name in Windows:
            if ua.find(name) != -1:
                j["os_name"] = Windows[name]
        for name in Browser:
            if ua.find(name) != -1:
                j["agent_name"] = Browser[name]
        return j

settings = {
        "template_path": os.path.join(os.path.dirname(__file__), "templates"),
        "debug": True,
}

# application should be an instance of `tornado.web.Application`,
# and don't wrap it with `sae.create_wsgi_app`
application = tornado.web.Application([
        (r'/', WelcomeHandler),
    ], **settings)



效果如下:



本文标签: 客户端浏览器操作系统手机信息