user_agents解析

安装

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)

你可能感兴趣的:(数学建模)