Home Web Front-end JS Tutorial Python crawler browser identification library

Python crawler browser identification library

Mar 12, 2018 am 11:34 AM
firefox python webkit

This time I will bring you Pythoncrawler browser logo library. What are the precautions for using the Python crawler browser logo library? Here is a practical case. Let’s take a look. .

   UserAgent.py
class toObj(object):
   def init(self, d):
        for a, b in d.items():            if isinstance(b, (list, tuple)):
                setattr(self, a, [toObj(x) if isinstance(
                    x, dict) else x for x in b])            else:
                setattr(self, a, toObj(b) if isinstance(b, dict) else b)                
'''
Android 设备
''' Android = toObj({    "Xiaomi": {        "Id": "Xiaomi",        "Name": "小米手机",        "UserAgent": "Mozilla/5.0 (Linux; U; Android 4.1.1; zh-cn;  MI2 Build/JRO03L) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 XiaoMi/MiuiBrowser/1.0"
    },    "Meizu": {        "Id": "Meizu",        "Name": "魅族手机",        "UserAgent": "JUC (Linux; U; 2.3.5; zh-cn; MEIZU MX; 640*960) UCWEB8.5.1.179/145/33232"
    },    "Nexus7": {        "Id": "Nexus7",        "Name": "Nexus 7 (Tablet)",        "UserAgent": "Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166  Safari/535.19"
    },    "AndroidGalaxyS3": {        "Id": "AndroidGalaxyS3",        "Name": "Samsung Galaxy S3 (Handset)",        "UserAgent": "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"
    },    "AndroidGalaxyTab": {        "Id": "AndroidGalaxyTab",        "Name": "Samsung Galaxy Tab (Tablet)",        "UserAgent": "Mozilla/5.0 (Linux; U; Android 2.2; en-gb; GT-P1000 Build/FROYO) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1"
    }
})'''
国产浏览器
'''China = toObj({    "360se": {        "Id": "360se",        "Name": "360安全浏览器",        "UserAgent": "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)"
    },    "360chrome": {        "Id": "360chrome",        "Name": "360极速浏览器",        "UserAgent": "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360Chrome)"
    },    "liebao": {        "Id": "liebao",        "Name": "猎豹浏览器",        "UserAgent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36 LBBROWSER"
    },    "ucpc": {        "Id": "ucpc",        "Name": "UC浏览器电脑版",        "UserAgent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 UBrowser/5.1.2238.18 Safari/537.36"
    },    "uc": {        "Id": "uc",        "Name": "UC浏览器手机版",        "UserAgent": "UCWEB/2.0 (iOS; U; iPh OS 4_3_2; zh-CN; iPh4) U2/1.0.0 UCBrowser/8.6.0.199 U2/1.0.0 Mobile"
    }, "sougou": {        "Id": "sougou",        "Name": "搜狗浏览器",        "UserAgent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36 SE 2.X MetaSr 1.0"
    }, "baidu": {        "Id": "baidu",        "Name": "百度浏览器",        "UserAgent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 BIDUBrowser/7.5 Safari/537.36"
    }, "maxthon": {        "Id": "maxthon",        "Name": "遨游浏览器",        "UserAgent": "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)"
    }, "qq": {        "Id": "qq",        "Name": "QQ浏览器",        "UserAgent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.124 Safari/537.36 QQBrowser/9.0.2229.400"
    }, "mqq": {        "Id": "mqq",        "Name": "QQ浏览器手机版",        "UserAgent": "MQQBrowser/3.6/Adr (Linux; U; 4.0.3; zh-cn; HUAWEI U8818 Build/U8818V100R001C17B926;480*800)"
    }, "wechat": {        "Id": "wechat",        "Name": "微信内置浏览器",        "UserAgent": "Mozilla/5.0 (iPhone; CPU iPhone OS 6_1_3 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Mobile/10B329 MicroMessenger/5.0.1"
    }
})'''
搜索引擎浏览器
'''Spider = toObj({    "Baidu": {        "Id": "Baidu",        "Name": "百度PC",        "UserAgent": "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)"
    },    "Baidum": {        "Id": "Baidum",        "Name": "百度移动端",        "UserAgent": "Mozilla/5.0 (Linux;u;Android 4.2.2;zh-cn;) AppleWebKit/534.46 (KHTML,like Gecko) Version/5.1 Mobile Safari/10600.6.3 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)"
    },    "BingBot": {        "Id": "BingBot",        "Name": "BingBot (Bing's spider)",        "UserAgent": "Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)"
    },    "Googlebot": {        "Id": "Googlebot",        "Name": "Googlebot (Google's spider)",        "UserAgent": "Googlebot/2.1 (+http://www.googlebot.com/bot.html)"
    },    "Slurp": {        "Id": "Slurp",        "Name": "Slurp! (Yahoo's spider)",        "UserAgent": "Mozilla/5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)"
    }
})'''
Mac OS
'''Safari = toObj({    "SafariMac": {        "Id": "SafariMac",        "Name": "Safari on Mac",        "UserAgent": "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; en-US) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27"
    },    "SafariWin": {        "Id": "SafariWin",        "Name": "Safari on Windows",        "UserAgent": "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27"
    },    "SafariiPad": {        "Id": "SafariiPad",        "Name": "Safari on iPad",        "UserAgent": "Mozilla/5.0 (iPad; CPU OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3"
    },    "SafariiPhone": {        "Id": "SafariiPhone",        "Name": "Safari on iPhone",        "UserAgent": "Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3"
    }
})'''
Opera 欧朋
'''Opera = toObj({    "OperaMac": {        "Id": "OperaMac",        "Name": "Opera on Mac",        "UserAgent": "Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.9.168 Version/11.52"
    },    "OperaWin": {        "Id": "OperaWin",        "Name": "Opera on Windows",        "UserAgent": "Opera/9.80 (Windows NT 6.1; WOW64; U; en) Presto/2.10.229 Version/11.62"
    }
})'''
Chrome
'''Chrome = toObj({    "ChromeAndroidMobile": {        "Id": "ChromeAndroidMobile",        "Name": "Chrome on Android Mobile",        "UserAgent": "Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19"
    },    "ChromeAndroidTablet": {        "Id": "ChromeAndroidTablet",        "Name": "Chrome on Android Tablet",        "UserAgent": "Mozilla/5.0 (Linux; Android 4.1.2; Nexus 7 Build/JZ054K) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19"
    },    "ChromeMac": {        "Id": "ChromeMac",        "Name": "Chrome on Mac",        "UserAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36"
    },    "ChromeUbuntu": {        "Id": "ChromeUbuntu",        "Name": "Chrome on Ubuntu",        "UserAgent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Ubuntu/11.10 Chromium/27.0.1453.93 Chrome/27.0.1453.93 Safari/537.36"
    },    "ChromeWin": {        "Id": "ChromeWin",        "Name": "Chrome on Windows",        "UserAgent": "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36"
    },    "ChromeiPhone": {        "Id": "ChromeiPhone",        "Name": "Chrome on iPhone",        "UserAgent": "Mozilla/5.0 (iPhone; CPU iPhone OS 6_1_4 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) CriOS/27.0.1453.10 Mobile/10B350 Safari/8536.25"
    }
})'''
IE
'''IE = toObj({    "IE10": {        "Id": "IE10",        "Name": "Internet Explorer 10",        "UserAgent": "Mozilla/5.0 (compatible; WOW64; MSIE 10.0; Windows NT 6.2)"
    },    "IE6": {        "Id": "IE6",        "Name": "Internet Explorer 6",        "UserAgent": "Mozilla/4.0 (Windows; MSIE 6.0; Windows NT 5.2)"
    },    "IE7": {        "Id": "IE7",        "Name": "Internet Explorer 7",        "UserAgent": "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)"
    },    "IE8": {        "Id": "IE8",        "Name": "Internet Explorer 8",        "UserAgent": "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)"
    },    "IE9": {        "Id": "IE9",        "Name": "Internet Explorer 9",        "UserAgent": "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"
    }
})'''
Firefox
'''Firefox = toObj({    "FFAndroidHandset": {        "Id": "FFAndroidHandset",        "Name": "Firefox on Android Mobile",        "UserAgent": "Mozilla/5.0 (Android; Mobile; rv:14.0) Gecko/14.0 Firefox/14.0"
    },    "FFAndroidTablet": {        "Id": "FFAndroidTablet",        "Name": "Firefox on Android Tablet",        "UserAgent": "Mozilla/5.0 (Android; Tablet; rv:14.0) Gecko/14.0 Firefox/14.0"
    },    "FFMac": {        "Id": "FFMac",        "Name": "Firefox on Mac",        "UserAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:21.0) Gecko/20100101 Firefox/21.0"
    },    "FFUbuntu": {        "Id": "FFUbuntu",        "Name": "Firefox on Ubuntu",        "UserAgent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:21.0) Gecko/20130331 Firefox/21.0"
    },    "FFWin": {        "Id": "FFWin",        "Name": "Firefox on Windows",        "UserAgent": "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0"
    }
})'''
Windows Phone
'''WinPhone = toObj({    "Win7Phone": {        "Id": "Win7Phone",        "Name": "Windows Phone 7",        "UserAgent": "Mozilla/4.0 (compatible; MSIE 7.0; Windows Phone OS 7.0; Trident/3.1; IEMobile/7.0; LG; GW910)"
    },    "Win75Phone": {        "Id": "Win75Phone",        "Name": "Windows Phone 7.5",        "UserAgent": "Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; SAMSUNG; SGH-i917)"
    },    "Win8Phone": {        "Id": "Win8Phone",        "Name": "Windows Phone 8",        "UserAgent": "Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch; NOKIA; Lumia 920)"
    }
})'''
iOS
'''iOS = toObj({    "iPad": {        "Id": "iPad",        "Name": "iPad",        "UserAgent": "Mozilla/5.0 (iPad; CPU OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3"
    },    "iPhone": {        "Id": "iPhone",        "Name": "iPhone",        "UserAgent": "Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3"
    },    "iPod": {        "Id": "iPod",        "Name": "iPod",        "UserAgent": "Mozilla/5.0 (iPod; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/3A101a Safari/419.3"
    }
})
Other = toObj({    "BlackBerry": {        "Id": "BlackBerry",        "Name": "BlackBerry - Playbook 2.1",        "UserAgent": "Mozilla/5.0 (PlayBook; U; RIM Tablet OS 2.1.0; en-US) AppleWebKit/536.2+ (KHTML, like Gecko) Version/7.2.1.0 Safari/536.2+"
    },    "MeeGo": {        "Id": "MeeGo",        "Name": "MeeGo - Nokia N9",        "UserAgent": "Mozilla/5.0 (MeeGo; NokiaN9) AppleWebKit/534.13 (KHTML, like Gecko) NokiaBrowser/8.5.0 Mobile Safari/534.13"
    }
})
Copy after login

Default = Chrome.ChromeWin.UserAgent

Use:

import  UserAgent as uaimport requests
print("默认标识",ua.Default)
print("小米标识",ua.Android.Xiaomi.UserAgent)
_headers={    'Accept-Language': 'zh-CN,zh;q=0.8',    'Content-Type': 'text/html;Charset=utf-8',    "User-Agent":ua.Android.Xiaomi.UserAgent
}
rd = requests.get("http://www.jianshu.com/", params=None, headers=_headers)
rd.encoding = 'utf-8'print(rd.text)
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related matters on the php Chinese website article!

Related reading:

Nodejs How to use http module to write upload image interface test client

h5 Make performance change line chart

The above is the detailed content of Python crawler browser identification library. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Python: Games, GUIs, and More Python: Games, GUIs, and More Apr 13, 2025 am 12:14 AM

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

How debian readdir integrates with other tools How debian readdir integrates with other tools Apr 13, 2025 am 09:42 AM

The readdir function in the Debian system is a system call used to read directory contents and is often used in C programming. This article will explain how to integrate readdir with other tools to enhance its functionality. Method 1: Combining C language program and pipeline First, write a C program to call the readdir function and output the result: #include#include#include#includeintmain(intargc,char*argv[]){DIR*dir;structdirent*entry;if(argc!=2){

Python and Time: Making the Most of Your Study Time Python and Time: Making the Most of Your Study Time Apr 14, 2025 am 12:02 AM

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Nginx SSL Certificate Update Debian Tutorial Nginx SSL Certificate Update Debian Tutorial Apr 13, 2025 am 07:21 AM

This article will guide you on how to update your NginxSSL certificate on your Debian system. Step 1: Install Certbot First, make sure your system has certbot and python3-certbot-nginx packages installed. If not installed, please execute the following command: sudoapt-getupdatesudoapt-getinstallcertbotpython3-certbot-nginx Step 2: Obtain and configure the certificate Use the certbot command to obtain the Let'sEncrypt certificate and configure Nginx: sudocertbot--nginx Follow the prompts to select

How to configure HTTPS server in Debian OpenSSL How to configure HTTPS server in Debian OpenSSL Apr 13, 2025 am 11:03 AM

Configuring an HTTPS server on a Debian system involves several steps, including installing the necessary software, generating an SSL certificate, and configuring a web server (such as Apache or Nginx) to use an SSL certificate. Here is a basic guide, assuming you are using an ApacheWeb server. 1. Install the necessary software First, make sure your system is up to date and install Apache and OpenSSL: sudoaptupdatesudoaptupgradesudoaptinsta

GitLab's plug-in development guide on Debian GitLab's plug-in development guide on Debian Apr 13, 2025 am 08:24 AM

Developing a GitLab plugin on Debian requires some specific steps and knowledge. Here is a basic guide to help you get started with this process. Installing GitLab First, you need to install GitLab on your Debian system. You can refer to the official installation manual of GitLab. Get API access token Before performing API integration, you need to get GitLab's API access token first. Open the GitLab dashboard, find the "AccessTokens" option in the user settings, and generate a new access token. Will be generated

What service is apache What service is apache Apr 13, 2025 pm 12:06 PM

Apache is the hero behind the Internet. It is not only a web server, but also a powerful platform that supports huge traffic and provides dynamic content. It provides extremely high flexibility through a modular design, allowing for the expansion of various functions as needed. However, modularity also presents configuration and performance challenges that require careful management. Apache is suitable for server scenarios that require highly customizable and meet complex needs.

See all articles