GolangNote

Golang笔记

Golang 根据 UserAgent 识别浏览器、设备和平台

Permalink

在许多应用中,需要从客户端 UserAgent 字符串中检测浏览器、设备和平台。比如以下用例:

  • 希望为不同的浏览器、设备或平台呈现不同的 HTML
  • 想为抓取机器人和社交媒体网站渲染 OG 标签
  • 希望记录用于分析的浏览器、设备或平台
  • 希望当 Google 机器人抓取您的网站进行 SEO 时,您的后端行为会有所不同

browser 库刚好适合

使用示例

Go:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
b, err := browser.NewBrowser("Mozilla/5.0 (Linux; Android 10; SM-A205U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.210 Mobile Safari/537.36")
 if err != nil {
  // handle error
 }

 // browser level information
 fmt.Println(b.Name())           // Chrome
 fmt.Println(b.Version())        // 90.0.4430.210
 fmt.Println(b.ShortVersion())   // 90
 fmt.Println(b.IsBrowserKnown()) // true
 fmt.Println(b.IsChrome())       // true

 // device level information
 fmt.Println(b.Device().Name())      // Samsung SM-A205U
 fmt.Println(b.Device().IsTablet())  // false
 fmt.Println(b.Device().IsSamsung()) // true

 // platform level information
 fmt.Println(b.Platform().Name())         // Android
 fmt.Println(b.Platform().Version())      // 10
 fmt.Println(b.Platform().IsAndroidApp()) // false

// bot level information
fmt.Println(b.Bot().Name())  // ""
fmt.Println(b.Bot().IsBot()) // false

支持的浏览器

plaintext:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Nokia
Alipay
UCBrowser
BlackBerry
Brave
Opera
Otter
Instagram
Snapchat
Weibo
MicroMessenger
QQ
Electron
DuckDuckGo
GoogleSearchApp
HuaweiBrowser
Konqueror
Maxthon
MiuiBrowser
PaleMoon
Puffin
Edge
InternetExplorer
SamsungBrowser
SogouBrowser
Vivaldi
VivoBrowser
Sputnik
YaaniBrowser
Yandex
Firefox
Chrome
Safari

支持的平台

plaintext:
1
2
3
4
5
6
7
8
9
10
11
12
AdobeAir
BlackBerry
KaiOS
IOS
WatchOS
WindowsMobile
WindowsPhone
Windows
Android
Linux
FirefoxOS
ChromeOS

支持的设备

plaintext:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
BlackberryPlaybook
Iphone
Ipad
IpodTouch
KindleFire
Kindle
PlayStation3
PlayStation4
PlayStation5
PSVita
PSP
WiiU
Wii
XboxOne
Xbox360
Switch
Surface
Kindle
Samsung
TV
Android

本文网址: https://golangnote.com/topic/321.html 转摘请注明来源

Related articles

Golang 生成防识别的图片验证码

验证码 captcha 是对抗密码强力破解、垃圾信息的有效方式,一般用于用户注册、登录,当检测到频繁发帖时也会启用验证码。下面介绍用golang 生成防机器识别的图片验证码。...

Golang Range 的性能提升Tip

Go 语言里使用 range 可以方便遍历数组(array)、切片(slice)、字典(map)和信道(chan)。这里主要关注他们的性能。...

Write a Comment to "Golang 根据 UserAgent 识别浏览器、设备和平台"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.22.3 Processed in 0ms