Python UserAgent解析库——user_agents,判断手机等访问设备





本文代码下载



简介

user_agents 是Python的UserAgent解析库,通过解析浏览器或HTTP的UserAgent字符串,检测访问设备如手机、平板电脑及是否具备触摸能力。

在这里插入图片描述




安装

安装

pip install user-agents




初试

from user_agents import parse

# iPhone的UserAgent
ua_string = 'Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B179 Safari/7534.48.3'
user_agent = parse(ua_string)

# 浏览器属性
print(user_agent.browser)  # Browser(family='Mobile Safari', version=(5, 1), version_string='5.1')
print(user_agent.browser.family)  # 'Mobile Safari'
print(user_agent.browser.version)  # (5, 1)
print(user_agent.browser.version_string)  # '5.1'

# 操作系统属性
print(user_agent.os)  # OperatingSystem(family=u'iOS', version=(5, 1), version_string='5.1')
print(user_agent.os.family)  # 'iOS'
print(user_agent.os.version)  # (5, 1)
print(user_agent.os.version_string)  # '5.1'

# 设备属性
print(user_agent.device)  # Device(family=u'iPhone', brand=u'Apple', model=u'iPhone')
print(user_agent.device.family)  # 'iPhone'
print(user_agent.device.brand)  # 'Apple'
print(user_agent.device.model)  # 'iPhone'

# 美观的字符串版本
print(str(user_agent))  # "iPhone / iOS 5.1 / Mobile Safari 5.1"




判断访问设备

  • is_mobile: 是否手机 (iPhone、Android、Blackberry、Windows Phone等)
  • is_tablet: 是否平板 (iPad、Kindle、Nexus等)
  • is_pc: 是否传统桌面操作系统 (Windows、OS X、Linux)
  • is_touch_capable: 是否有触摸功能
  • is_bot: 是否搜索引擎爬虫
from user_agents import parse

# 无触摸功能的黑莓设备
ua_string = 'BlackBerry9700/5.0.0.862 Profile/MIDP-2.1 Configuration/CLDC-1.1 VendorID/331 UNTRUSTED/1.0 3gpp-gba'
user_agent = parse(ua_string)
print(user_agent.is_mobile)  # True
print(user_agent.is_tablet)  # False
print(user_agent.is_touch_capable)  # False
print(user_agent.is_pc)  # False
print(user_agent.is_bot)  # False
print(str(user_agent))  # "BlackBerry 9700 / BlackBerry OS 5 / BlackBerry 9700"

# 三星Galaxy S3
ua_string = 'Mozilla/5.0 (Linux; U; Android 4.0.4; en-gb; GT-I9300 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30'
user_agent = parse(ua_string)
print(user_agent.is_mobile)  # True
print(user_agent.is_tablet)  # False
print(user_agent.is_touch_capable)  # True
print(user_agent.is_pc)  # False
print(user_agent.is_bot)  # False
print(str(user_agent))  # "Samsung GT-I9300 / Android 4.0.4 / Android 4.0.4"

# iPad
ua_string = 'Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10'
user_agent = parse(ua_string)
print(user_agent.is_mobile)  # False
print(user_agent.is_tablet)  # True
print(user_agent.is_touch_capable)  # True
print(user_agent.is_pc)  # False
print(user_agent.is_bot)  # False
print(str(user_agent))  # "iPad / iOS 3.2 / Mobile Safari 4.0.4"

# Kindle
ua_string = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-us; Silk/1.1.0-80) AppleWebKit/533.16 (KHTML, like Gecko) Version/5.0 Safari/533.16 Silk-Accelerated=true'
user_agent = parse(ua_string)
print(user_agent.is_mobile)  # False
print(user_agent.is_tablet)  # True
print(user_agent.is_touch_capable)  # True
print(user_agent.is_pc)  # False
print(user_agent.is_bot)  # False
print(str(user_agent))  # "Kindle / Android / Amazon Silk 1.1.0-80"

# 带触摸功能的Windows 8设备
ua_string = 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0; Touch)'
user_agent = parse(ua_string)
print(user_agent.is_mobile)  # False
print(user_agent.is_tablet)  # False
print(user_agent.is_touch_capable)  # True
print(user_agent.is_pc)  # True
print(user_agent.is_bot)  # False
print(str(user_agent))  # "PC / Windows 8 / IE 10"




封装

判断手机、平板电脑、PC

import user_agents


def ismobile(ua_string):
    '''是否手机

    :param ua_string: UserAgent字符串
    :return: True or False

    >>> ismobile('BlackBerry9700/5.0.0.862 Profile/MIDP-2.1 Configuration/CLDC-1.1 VendorID/331 UNTRUSTED/1.0 3gpp-gba')
    True
    >>> ismobile('Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0; Touch)')
    False
    '''
    return user_agents.parse(ua_string).is_mobile


def istablet(ua_string):
    '''是否平板

    :param ua_string: UserAgent字符串
    :return: True or False

    >>> istablet('Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10')
    True
    >>> istablet('Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0; Touch)')
    False
    '''
    return user_agents.parse(ua_string).is_tablet


def ispc(ua_string):
    '''是否PC

    :param ua_string: UserAgent字符串
    :return: True or False

    >>> ispc('Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0; Touch)')
    True
    >>> ispc('BlackBerry9700/5.0.0.862 Profile/MIDP-2.1 Configuration/CLDC-1.1 VendorID/331 UNTRUSTED/1.0 3gpp-gba')
    False
    '''
    return user_agents.parse(ua_string).is_pc




随机生成User-Agent

安装

pip install fake-useragent

代码

from fake_useragent import UserAgent

ua = UserAgent()

print(ua.ie)
print(ua.msie)
print(ua['Internet Explorer'])
print(ua.opera)
print(ua.chrome)
print(ua.google)
print(ua['google chrome'])
print(ua.firefox)
print(ua.ff)
print(ua.safari)

print(ua.random)

推荐阅读:fake-useragent




参考文献

  1. 根据userAgent判断客户端是否手机、操作系统、浏览器等信息
  2. fake-useragent: up to date simple useragent faker with real world database
  3. 最新UserAgent——UserAgentString.com
  4. python-user-agents: A Python library that provides an easy way to identify devices like mobile phones, tablets and their capabilities by parsing (browser) user agent strings.
  5. Apple devices - Flat icons (PSD)
一个简单的User-Agent库,可以从一条User-Agent字符串获取该用户的相关信息。 支持平台: Node.JS / (Windows) Classical ASP / (Windows) WScript / (Windows) CScript / Internet Explorer 6 / Google Chrome / Mozilla Firefox / Apple Safari 安装方式:npm: $ npm install useragent.js bower: $ bower install useragent.js支持检测列表: Tested Browsers: 114Browser / 115Browser / 2345Chrome / 2345Explorer / 360 Aphone Browser / 360 Explorer / Abolimba / Acoo Browser / Alienforce / Amaya / Amazon Silk / America Online Browser / Amiga / Android Webkit / AOL / Arora / Atomic Web Browser / Avant Browser / Baidu Browser / Barca Proxxxx / BarcaC3 / Beamrise / Beonex / BlackBerry / Blackbird / BlackHawk / Blazer / Bolt / BonEchob2 / BrowseX / Browzar / Bunjalloo / Camino / Charon / Cheshire / Chimera / Chrome Mobile / ChromePlus / Chromium / Classilla / Coast / Columbus / CometBird / Comodo Dragon / Conkeror / CoolNovo / CoRom / Crazy Browser / curl / Cyberdog / Deepnet Explorer / Demeter / DeskBrowse / Dillo / DoCoMo / DocZilla / Dooble / Doris / Dorothy / Edbrowse / Element Browser / Elinks / Enigma / Epic / Epiphany / Escape / Fennec / Firebird / Firefox / Fireweb Navigator / Flock / Fluid / Galaxy / Galaxy Nexus / Galeon / GlobalMojo / GNU IceCat / GO Browser / Google Chrome / Google Chrome Frame / Google CriOS / GoSurf / GranParadiso / GreenBrowser / Gtk WebCore / Hana / HotJava / Hv3 Build / IBM WebExplorer / IBrowse / iCab / Iceape / IceBrowser v6 / IceWeasel / IEMobile / iNet Browser / Internet Explorer / Internet Explorer Spartan / InternetSurfboard / iRider / Iris / JuziBrowser / Kapiko / Kazehakase / Kirix Strata / KKman / K-Meleon / KMLite / K-Ninja / Konqueror / LBrowser / LeechCraft / Liebao Browser / Liebaofast / Links / Lobo / lolifox / Lorentz / Lunascape / Lynx / Madfox / Maemo Browser / Maple Browser / Maxthon / Maxthon / MIB / Midori / Midori / Minefieldb4pre / Minimo / MiuiBrowser / Mobile Safari / Mosaic / Mozilla Developer Preview / MQQBrowser / Multi-Browser XP / MultiZilla / MxNitro / myibrowalpha2 /
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XerCis

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值