(转)python-user-agents
原文:http://blog.topspeedsnail.com/archives/1958
Python3网络爬虫(四):使用User Agent和代理IP隐藏身份-------https://blog.csdn.net/c406495762/article/details/60137956
python3 网络爬虫(五)scrapy中使用User-Agent-----------------https://blog.csdn.net/Fight_Huang/article/details/76650972
user_agents提供了一个简单的方法来判断用户设备(手机、平板..)和使用什么类型的浏览器。它是基于ua-parser的。
安装:
|
1
|
pip install pyyaml ua-parser user-agents
|
使用:
|
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
|
>>> from user_agents import parse
>>> 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)
>>> user_agent.browser
Browser(family=u'Mobile Safari', version=(5, 1), version_string='5.1')
>>> user_agent.browser.family
u'Mobile Safari'
>>> user_agent.browser.version
(5, 1)
>>> user_agent.browser.version_string
'5.1'
>>> # 操作系统属性
>>> user_agent.os
OperatingSystem(family=u'iOS', version=(5, 1), version_string='5.1')
>>> user_agent.os.family
u'iOS'
>>> user_agent.os.version
(5, 1)
>>> user_agent.os.version_string
'5.1'
>>> # 设备属性
>>> user_agent.device
Device(family=u'iPhone', brand=u'Apple', model=u'iPhone')
>>> user_agent.device.family
u'iPhone'
>>> user_agent.device.brand
u'Apple'
>>> user_agent.device.model
u'iPhone'
>>>
>>> str(user_agent)
'iPhone / iOS 5.1 / Mobile Safari 5.1'
|
它还提供了属性判断:
- is_mobile:判断是不是手机
- is_tablet:判断是不是平板
- is_pc:判断是不是桌面系统
- is_touch_capable:有没有触屏功能
- is_bot:是不是搜索引擎的爬虫
例如:
|
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
|
>>> # 古老的黑莓手机
>>> 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)
>>> user_agent.is_mobile
True
>>> user_agent.is_tablet
False
>>> user_agent.is_touch_capable
False
>>> user_agent.is_pc
False
>>> user_agent.is_bot
False
>>> str(user_agent)
'BlackBerry 9700 / BlackBerry OS 5 / BlackBerry 9700'
>>>
>>> # android 手机
>>> 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)
>>> user_agent.is_mobile
True
>>> user_agent.is_tablet
False
>>> user_agent.is_touch_capable
True
>>> user_agent.is_pc
False
>>> user_agent.is_bot
False
>>> str(user_agent)
'Samsung GT-I9300 / Android 4.0.4 / Android 4.0.4'
>>>
|
(转)python-user-agents的更多相关文章
- k8s搭建链路监控:skywalking
skywalking架构及简介 官网:https://github.com/apache/skywalking 简介 Java, .NET Core, NodeJS, PHP, and Python ...
- Introduction of python
"Life is short, you need Python!" Python (British pronunciation:/ˈpaɪθən/ American pronunc ...
- The novaclient Python API
The novaclient Python API Usage First create a client instance with your credentials: >>> f ...
- Python渗透测试工具合集
摘自:http://www.freebuf.com/tools/94777.html 如果你热爱漏洞研究.逆向工程或者渗透测试,我强烈推荐你使用 Python 作为编程语言.它包含大量实用的库和工具, ...
- Think Python - Chapter 17 - Classes and methods
17.1 Object-oriented featuresPython is an object-oriented programming language, which means that it ...
- python瓦登尔湖词频统计
#瓦登尔湖词频统计: import string path = 'D:/python3/Walden.txt' with open(path,'r',encoding= 'utf-8') as tex ...
- Python金融行业必备工具
有些国外的平台.社区.博客如果连接无法打开,那说明可能需要"科学"上网 量化交易平台 国内在线量化平台: BigQuant - 你的人工智能量化平台 - 可以无门槛地使用机器学习. ...
- Serpent.AI – 游戏代理框架(Python)
Serpent.AI - 游戏代理框架(Python) Serpent.AI是一个简单而强大的新颖框架,可帮助开发人员创建游戏代理.将您拥有的任何视频游戏变成一个成熟的实验的沙箱环境,所有这些都是熟悉 ...
- PySC2是DeepMind的“星际争霸II学习环境”(SC2LE)的Python组件
PySC2是DeepMind的"星际争霸II学习环境"(SC2LE)的Python组件. 它暴露了暴雪娱乐公司的星际争霸II机器学习API作为Python RL环境. 这是Deep ...
- Python菜鸟快乐游戏编程_pygame(6)
Python菜鸟快乐游戏编程_pygame(博主录制,2K分辨率,超高清) https://study.163.com/course/courseMain.htm?courseId=100618802 ...
随机推荐
- Win10通知区域图标设置;windows10系统图标合并;Windows10系统通知合并
1.一直喜欢Windows7的通知图标合并 2.通过查阅找到方法 a.输入命令语句 win+R :shell:::{05d7b0f4-2121-4eff-bf6b-ed3f69b894d9} 回 ...
- 天使投资、A轮、B轮、C轮
一般是这样划分的. A轮融资:公司产品有了成熟模样,开始正常运作一段时间并有完整详细的商业及盈利模式,在行业内拥有一定地位和口碑.公司可能依旧处于亏损状态.资金来源一般是专业的风险投资机构(VC).投 ...
- Java数据类型、操作符、表达式
基本与C#相同,因C#从Java学的 如操作符 对象的Equals方法,比较两个对象的内容是否相等. ==是比较是否引用同一对象.
- 【20171101】the first day in a new company
英文差的要命还飙英文,擦嘞!!! 就是想记录下 点滴 如下配图: | | | V 手动配图这是图!!!
- hdu 4902 线段树+逆向模拟
http://acm.hdu.edu.cn/showproblem.php?pid=4902 出n个数,然后对这n个数进行两种操作: 如果是 1 l r x,则把 [l, r] 区间里面的每一个数都变 ...
- ES版本控制
版本控制 ElasticSearch采用了乐观锁来保证数据的一致性,也就是说,当用户对document进行操作时,并不需要对该doucument作加锁和解锁的操作,只需要指定要操作的版本即可.当版本号 ...
- 安装json插件
谷歌浏览器中安装JsonView扩展程序 实际开发工作中经常用到json数据,那么就会有这样一个需求:在谷歌浏览器中访问URL地址返回的json数据能否按照json格式展现出来. 比如,在谷歌浏览器中 ...
- applicationContext.xml 基本配置
<!-- 头文件,主要注意一下编码 --><?xml version="1.0" encoding="UTF-8"?><beans ...
- [leetcode 12] Inter to Roman
1 题目: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...
- 基于tkinter的九型人格测试系统介绍
基于tkinter的九型人格测试系统介绍 一.程序代码地址,GitHub 二.程序介绍 1.login.py 登录界面: 注册界面: 2.mainWindow.py 登录成功之后的界面: 3.doTe ...