原文: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. git,版本控制教程

    主要内容 版本回退 工作区和暂存区 管理修改 撤销修改 删除文件 分支管理     *****此处没有深讲***** 一.两条基本查看命名 查看状态命令: git status 查看修改内容命令: g ...

  2. oracle 11g中文乱码解决的办法一

    oracle中文乱码解决的办法 正常安装完oracle客户端后,插入数据时,发现中文都是问号. PLL/SQL中输入select userenv('LANGUAGE') From Dual,结果为AM ...

  3. hibernate映射组成关系

    目录结构 类 package com.hibernate.helloworld; public class School { private String name; private String a ...

  4. Arcgis Server Manager发布ArcGISTiledMapServiceLayer服务

    用ArcgisServer manager发布一个ArcGISDynamicMapServiceLayer服务: Add New Service->填写name,type 选择Map servi ...

  5. (完全背包) Piggy-Bank (hdu 1114)

    题目大意:              告诉你钱罐的初始重量和装满的重量, 你可以得到这个钱罐可以存放钱币的重量,下面有 n 种钱币, n 组, 每组告诉你这种金币的价值和它的重量,问你是否可以将这个钱 ...

  6. STL容器(C11)--unordered_map用法

    http://www.cplusplus.com/reference/unordered_map/unordered_map/

  7. C++ 中的异常机制分析

    C++异常机制概述 异常处理是C++的一项语言机制,用于在程序中处理异常事件.异常事件在C++中表示为异常对象.异常事件发生时,程序使用throw关键字抛出异常表达式,抛出点称为异常出现点,由操作系统 ...

  8. Scala_控制结构

    控制结构 if条件表达式  val x = 6  if (x>0){    println("This is a positive number") }else if(x== ...

  9. PHP内存溢出Allowed memory size of 解决办法

    PHP内存溢出Allowed memory size of 解决办法 博客分类: php   ============================Allowed memory size of  x ...

  10. fping常用参数介绍

    fping的主要参数有以下两个: -a:只显示存活主机: -u:只显示不存活主机: -l:循环ping目标IP地址的输入方式: fping IP1 IP2 IP3 ...: fping -f file ...