原文: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. myBatis中if test 字符串注意事项

    错误写法: <if test="userName == 'boshen'"> AND `USER_NAME` = #{userName} </if> 正确写 ...

  2. readystatechange事件

    IE为DOM文档中的某些部分readystatechange事件. 这个事件的目的是提供与文档或元素的加载状态有关的信息,但是这个事件的行为有时候也很难预测. 支持readystatechange事件 ...

  3. thinkphp3.2 链接数据库测试

    配置数据库: 在Application/Home/config.php文件中设置: <?php return array( 'DB_TYPE' => 'mysql', // 数据库类型 ' ...

  4. FPGA速度等级

    转自http://wenku.baidu.com/view/ea793deef8c75fbfc77db263.html?from=rec 最初接触speed grade这个概念时,很是为Altera的 ...

  5. 工作总结(一):Linux C

    这三个月以来一直忙着赶进度,没有停下来记录一些东西,很多很好的东西往往只能零零散散地记在草稿本上, 这样偶尔想起来自己都找不到,所以现在抽空总结下来. 这些天做了三件事,其一是在Linux下开发了对接 ...

  6. sublime text2一些使用技巧+插件

    一.前言 作为一个前端,有一款好的开发利器是必不可少的,editplus.notepad++都是不错的工具,体积轻巧,启动迅速(dw太浮肿了).最近,又有一款新的编辑器诞生,席卷前端界,惹得无数喜爱, ...

  7. 为什么要用GCD-Swift2.x

    为什么要用GCD-Swift2.x 当今世界,多核已然普及.但是APP却不见得很好的跟上了这个趋势.APP 想要利用好多核就必须可以保证任务能有效的分配.并行执行可以让APP同时执行很多 的任务.这个 ...

  8. Shell编程-08-Shell中的循环语句

    目录 while语句 until语句 for语句 select语句 循环中断控制 循环语句总结     循环语句常用于重复执行一条命令或一组命令等,直到达到结束条件后,则终止执行.在Shell中常见的 ...

  9. bzoj 2440 完全平方数 【莫比乌斯函数】

    题目 题意:第Ki 个不是完全平方数的正整数倍的数. 对于一个数t,t以内的数里的非完全平方数倍数的个数:num=1的倍数的数量−一个质数平方数(9,25,49...)的倍数的数量+两个质数的积平方数 ...

  10. day02_雷神_字符串、列表、字典

    1.字符串 1.1 字符串相加 s1 = " ale x " s2 = " sb " print(s1 + s2) #识别空格 print(s1.strip() ...