標籤:cWeb style blog http io ar color os 使用
User Agent中文名為使用者代理程式,是Http協議中的一部分,屬於頭域的組成部分,User Agent也簡稱UA。它是一個特殊字元串頭,是一種向訪問網站提供你所使用的瀏覽器類型及版本、作業系統及版本、瀏覽器核心、等資訊的標識。通過這個標識,使用者所訪問的網站可以顯示不同的排版從而為使用者提供更好的體驗或者進行資訊統計;例如用手機訪問Google和電腦訪問是不一樣的,這些是Google根據訪問者的UA來判斷的。UA可以進行偽裝。(wiki)
分析瀏覽器的User-Agent 我們可以收集用戶端相關資訊:是否手機、作業系統、瀏覽器等資訊。
效果:http://1.billo.sinaapp.com/
以下為擷取json格式ua的函數
ua為字串格式的 User-Agent 欄位
import urllibimport urllib2import jsondef getUA(ua): ua = urllib.quote(ua) url = "http://www.useragentstring.com/?uas=%s&getJSON=all" %ua keys = { "Host":"www.useragentstring.com", "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判斷作業系統和瀏覽器類型的例子
#coding:utf-8import tornado.webimport sysimport urllibimport urllib2import jsonimport os.pathfrom tornado.httpclient import AsyncHTTPClientclass 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.com/?uas=%s&getJSON=all" % ua_url keys = { "Host":"www.useragentstring.com", "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 jsettings = { "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)
效果如下:
根據userAgent判斷用戶端是否手機、作業系統、瀏覽器等資訊