OpenResty 中使用 lua-resty-woothee 解析 User-Agent

OpenResty Lua LuaRocks About 963 words

项目介绍

lua-resty-wootheeWootheeOpenResty版本Woothee是一个多语言的User-Agent解析器。

开源地址:https://github.com/woothee/lua-resty-woothee

快速入门

安装依赖

luarocks install lua-resty-woothee

代码示例

local woothee = require "resty.woothee"

-- parse
local r = woothee.parse(ngx.var.http_user_agent)
--  => {"name": "xxx", "category": "xxx", "os": "xxx", "version": "xxx", "vendor": "xxx"}

-- crawler?
local crawler = woothee.is_crawler(ngx.var.http_user_agent)
--  => true

常用方法

result = woothee.parse(ua)

传入User-Agent字符串,返回Table类型结果。

crawler = woothee.is_crawler(ua)

判断是否是爬虫,返回布尔类型。

字段解释

{"name":"Chrome","category":"pc","os":"Windows 10","version":"79.0.3945.130","vendor":"Google","os_version":"NT 10.0"}
  • name:浏览器名称;
  • category:平台类型;
    • pc: 电脑
    • smartphone:智能手机
    • mobilephone:移动电话
    • appliance:应用
    • crawler:爬虫
    • misc:其他
    • unknown:未知
  • os:操作系统;
  • version:浏览器版本号;
  • os_version:操作系统版本号;
  • vendor:浏览器制造商;

备注

可以根据category是否为crawler来判断是否是爬虫,和is_crawler效果一样。

开源案例

https://github.com/fendoudebb/z-blog-openresty

Views: 5,541 · Posted: 2020-03-01

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh