原文: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的更多相关文章

  1. k8s搭建链路监控:skywalking

    skywalking架构及简介 官网:https://github.com/apache/skywalking 简介 Java, .NET Core, NodeJS, PHP, and Python ...

  2. Introduction of python

    "Life is short, you need Python!" Python (British pronunciation:/ˈpaɪθən/ American pronunc ...

  3. The novaclient Python API

    The novaclient Python API Usage First create a client instance with your credentials: >>> f ...

  4. Python渗透测试工具合集

    摘自:http://www.freebuf.com/tools/94777.html 如果你热爱漏洞研究.逆向工程或者渗透测试,我强烈推荐你使用 Python 作为编程语言.它包含大量实用的库和工具, ...

  5. Think Python - Chapter 17 - Classes and methods

    17.1 Object-oriented featuresPython is an object-oriented programming language, which means that it ...

  6. python瓦登尔湖词频统计

    #瓦登尔湖词频统计: import string path = 'D:/python3/Walden.txt' with open(path,'r',encoding= 'utf-8') as tex ...

  7. Python金融行业必备工具

    有些国外的平台.社区.博客如果连接无法打开,那说明可能需要"科学"上网 量化交易平台 国内在线量化平台: BigQuant - 你的人工智能量化平台 - 可以无门槛地使用机器学习. ...

  8. Serpent.AI – 游戏代理框架(Python)

    Serpent.AI - 游戏代理框架(Python) Serpent.AI是一个简单而强大的新颖框架,可帮助开发人员创建游戏代理.将您拥有的任何视频游戏变成一个成熟的实验的沙箱环境,所有这些都是熟悉 ...

  9. PySC2是DeepMind的“星际争霸II学习环境”(SC2LE)的Python组件

    PySC2是DeepMind的"星际争霸II学习环境"(SC2LE)的Python组件. 它暴露了暴雪娱乐公司的星际争霸II机器学习API作为Python RL环境. 这是Deep ...

  10. Python菜鸟快乐游戏编程_pygame(6)

    Python菜鸟快乐游戏编程_pygame(博主录制,2K分辨率,超高清) https://study.163.com/course/courseMain.htm?courseId=100618802 ...

随机推荐

  1. BZOJ 2120 数颜色 (带修莫队)

    2120: 数颜色 Time Limit: 6 Sec  Memory Limit: 259 MBSubmit: 6367  Solved: 2537[Submit][Status][Discuss] ...

  2. AngularJS实战之filter的使用一

    一.格式化数字为货币格式. <div>{{money|currency:"$"}}</div> <div>{{money|currency:&q ...

  3. poj 1094 Sorting It All Out 拓补排序

    Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...

  4. vue+大文件分片上传

    最近公司在使用vue做工程项目,实现大文件分片上传. 网上找了一天,发现网上很多代码都存在很多问题,最后终于找到了一个符合要求的项目. 工程如下: 对项目的大文件上传功能做出分析,怎么实现大文件分片上 ...

  5. 大道至简第一章和java理论学时第一节。感受。

    这周上了本学期的第一节java课程.课件上说了一些学习java的基本思想.举了个“愚公移山”的例子.这可能就像刚接触一门新的语言,来练习输出“HelloWorld”一样,已成惯例. “愚公移山”的这个 ...

  6. POJ2431--Expedition(优先队列)

    Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Bein ...

  7. noip第22课作业

    1.   数字分解 [问题描述] 任何一个大于1的自然数n,总可以拆分成若干个小于n的自然数之和,当n等于5时有6种拆分方法: 5=1+1+1+1+1 5=1+1+1+2 5=1+1+3 5=1+2+ ...

  8. ListView 指定显示最后一行,scrollView显示最底部

    实现方式: 一. mListView.setSelection(adapter.getCount()-1); 二.在ListView的xml添加以下属性: android:stackFromBotto ...

  9. 中大 9095. Islands

    9095. Islands 限制条件 时间限制: 2 秒, 内存限制: 256 兆 题目描述 Whenever it rains, Farmer John's field always ends up ...

  10. power designer简单教程

    1.file->new model 2.根据菜单或功能模型新建physical diagram(当然也可以新建package) 选中项目,右键——>new ->physical di ...