目录

前言

一、fake_agent是什么?

二、安装

1.安装

2.fake-useragent例子

2.1查看版本

 3.查看

4.获取页面

获取sogou的页面

通过浏览器查看 

总结



前言

Python - fake_useragent

数据是从useragentstring.com和包的一部分预先下载的

本地检索用户代理字符串支持Python 3.x

回退到外部资源(useragentstring.com)+缓存


一、fake_agent是什么?

UserAgent 就是用户代理,又叫报头,是一串字符串,相当于浏览器的身份证号
在利用爬虫爬取网站数据时,频繁更换它可以避免触发相应的反爬机制

二、安装

1.安装

pip install fake-useragent 安装简单,直接通过清华站点安装

pip install fake-useragent
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting fake-useragent
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/6a/07/559a096bb0e1a971ace2f42f103299152d0499c2a78506b6b3d5ffa28887/fake_useragent-1.1.1-py3-none-any.whl (50 kB)
     |████████████████████████████████| 50 kB 790 kB/s 

2.fake-useragent例子

2.1查看版本

from fake_useragent import UserAgent
ua = UserAgent()

print(ua.random)
print(ua.safari)
print(ua.google)
print(ua.opera)
print(ua.msie)
print(ua.ie)
print(ua.firefox)
Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.9200
Mozilla/5.0 (Macintosh; U; Intel Mac OS X; ru-ru) AppleWebKit/522.11.1 (KHTML, like Gecko) Version/3.0.3 Safari/522.12.1
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2762.73 Safari/537.36
Opera/5.11 (Windows 98; U)  [en]
Mozilla/5.0 (compatible; MSIE 10.0; Macintosh; Intel Mac OS X 10_7_3; Trident/6.0)
Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; .NET CLR 2.7.58687; SLCC2; Media Center PC 5.0; Zune 3.4; Tablet PC 3.6; InfoPath.3)
Mozilla/5.0 (Windows; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9

 3.查看

url = 'https://www.zhihu.com/question/54547736/answer/2534550597'
headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36"
}
response = requests.get(url=url, headers=headers)
print(response.status_code)

4.获取页面

import param as param
from fake_useragent import UserAgent
import requests

url = 'https://hanyu.sogou.com/?query=%E5%A4%A7%E5%99%A8%E6%99%9A%E6%88%90'
headers = {"User-Agent": UserAgent().chrome}

res = requests.get(url=url, headers=headers)

print(res.content)

f = open('test.html', 'w')
f.write(str(res.content))
f.close()

获取sogou的页面

b'<!DOCTYPE html><html lang="zh-cn" data-env="production" data-tag="hanyu-pc-20221129193338"><head><title>\xe6\x90\x9c\xe7\x8b\x97\xe6\xb1\x89\xe8\xaf\xad</title><meta charset="UTF-8"><meta name="viewport" content="width=device-width,minimum-scale=1,maximum-scale=1,user-scalable=no"><meta name="format-detection" content="telephone=no"><meta name="format-detection" content="address=no"><meta name="title" content="\xe6\x90\x9c\xe7\x8b\x97\xe6\xb1\x89\xe8\xaf\xad \xe2\x80\x94 \xe4\xbd\xa0\xe7\x9a\x84\xe6\xb1\x89\xe8\xaf\xad\xe5\xad\xa6\xe4\xb9\xa0\xe5\xa5\xbd\xe5\xb8\xae\xe6\x89\x8b"><meta name="keywords" content="\xe6\x90\x9c\xe7\x8b\x97 \xe6\xb1\x89\xe8\xaf\xad \xe6\x90\x9c\xe7\x8b\x97\xe6\xb1\x89\xe8\xaf\xad"><meta name="description" content="\xe6\x90\x9c\xe7\x8b\x97\xe6\xb1\x89\xe8\xaf\xad\xe4\xb8\xba\xe6\x82\xa8\xe6\x8f\x90\xe4\xbe\x9b\xe5\x85\x8d\xe8\xb4\xb9\xe3\x80\x81\xe6\x9d\x83\xe5\xa8\x81\xe7\x9a\x84\xe6\xb1\x89\xe8\xaf\xad\xe7\x9f\xa5\xe8\xaf\x86\xe6\x9c\x8d\xe5\x8a\xa1\xe3\x80\x82\xe5\x8c\x85\xe5\x90\xab\xe6\xb1\x89\xe5\xad\x97\xe3\x80\x81\xe8\xaf\x8d\xe8\xaf\xad\xe3\x80\x81\xe6\x88\x90\xe8\xaf\xad\xe3\x80\x81\xe8\xaf\x97\xe8\xaf\x8d\xe3\x80\x81\xe4\xbd\x9c\xe6\x96\x87\xe7\xad\x89\xe5\x85\xa8\xe7\xb1\xbb\xe5\x88\xab\xe5\x86\x85\xe5\xae\xb9\xe3\x80\x82"><meta itemprop="name"><meta itemprop="image"><meta itemprop="description" name="description"><meta name="reqinfo" content="uuid:ec01c60b-aa35-4111-b2ac-76c5eab80dd3, server:10.162.133.217, time:2022-12-23 22:00:24, iploc:CN3502, idc:hb"><meta name="renderer" content="webkit"><meta name="apple-mobile-web-app-capable" content="yes"><meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1"><meta http-equiv="cache-control" content="max-age=0"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="pragma" content="no-cache"><meta http-equiv="expires" content="0"><meta name="format-detection" content="telephone=no"><link type="image/x-icon" href="//dlweb.sogoucdn.com/translate/favicon.ico?v=20180424" rel="icon"><link type="image/x-icon" href="//dlweb.sogoucdn.com/translate/favicon.ico?v=20180424" rel="Shortcut Icon"><link type="image/x-icon" href="//dlweb.sogoucdn.com/translate/favicon.ico?v=20180424" rel="bookmark"><link rel="apple-touch-icon-precomposed" href="//dlweb.sogoucdn.com/translate/wap/static/img/logo_gray_80.png"><script>/**\n * https://tam.cdn-go.cn/aegis-sdk/v1.35.2/aegis.min.13.js\n * Last Release Time Thu Nov 25 2021 22:10:42 GMT+0800 (GMT+08:00)\n **/\n !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).Aegis=t()}(this,function(){"use strict";var t,n;function e(e){this.name="__st"+(1e9*Math.random()>>>0)+t+"__",null==e||e.forEach(this.add,this),t+=1}Array.prototype.find||Object.defineProperty(Array.prototype,"find",{configurable:!0,writable:!0,value:function(e){if(null===this)throw new TypeError(\'"this" is null or not defined\');var t=Object(this),n=t.length>>>0;if("function"!=typeof e)throw new TypeError("predicate must be a function");for(var i=arguments[1],o=0;o<n;){var r=t[o];if(e.call(i,r,o,t))return r;o+=1}}}),String.prototype.startsWith||Object.defineProperty(String.prototype,"startsWith",{value:function(e,t){return this.substring(t=!t||t<0?0:+t,t+e.length)===e}}),window.WeakSet||(t=Date.now()%1e9,e.prototype.add=function(e){var t=this.name;return e[t]||Object.defineProperty(e,t,{value:!0,writable:!0}),this},e.prototype.delete=function(e){return!!e[this.name]&&!(e[this.name]=void 0)},e.prototype.has=function(e){return!!e[this.name]},n=e,Object.defineProperty(window,"WeakSet",{value:function(e){return new n(e)}})),Object.assign||Object.defineProperty(Object,"assign",{enumerable:!1,configurable:!0,writable:!0,value:function(e){if(null==e)throw new TypeError("Cannot convert first argument to object");for(var t=Object(e),n=1;n<arguments.length;n++)if(null!=(i=arguments[n]))for(var i=Object(i),o=Object.keys(Object(i)),r=0,s=o.length;r<s;r++){var a=o[r],u=Object.getOwnPropertyDescriptor(i,a);null!=u&&u.enumerable&&(t[a]=i[a])}return t}});var i=function(e,t){return(i=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])})(e,t)},a=function(){return(a=Object.assign||function(e){for(var t,n=1,i=arguments.length;n<i;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e}).apply(this,arguments)};function u(){for(var e=0,t=0,n=arguments.length;t<n;t++)e+=arguments[t].length;for(var i=Array(e),o=0,t=0;t<n;t++)for(var r=arguments[t],s=0,a=r.length;s<a;s++,o++)i[o]=r[s];return i}function o(t){try{return encodeURIComponent(decodeURIComponent(t))}catch(e){return t}}var h=(s.prototype.indexOf=function(e,t){for(var n=0;n<e.length;n++)if(e[n].callback===t)return n;return-1},s.prototype.on=function(e,t,n){if(void 0===n&&(n=0),this){var i=this.eventsList[e];return(i||(this.eventsList[e]=[],i=this.eventsList[e]),-1!==this.indexOf(i,t))?this:(i.push({name:e,type:n||0,callback:t}),this)}},s.prototype.one=function(e,t){this.on(e,t,1)},s.prototype.remove=function(e,t){if(this){var n=this.eventsList[e];if(!n)return null;if(t)return n.length&&(t=this.indexOf(n,t),n.splice(t,1)),this;try{delete this.eventsList[e]}catch(e){}return null}},s.prototype.clear=function(){this.eventsList={}},s),r=function(e){return(e=Array.isArray(e)?e:[e]).map(function(t,n){return Object.getOwnPropertyNames(t).map(function(e){return o(e)+"["+n+"]="+(void 0===t[e]?"":o(t[e]))}).join("&")}).join("&")+(e.length?"&count="+e.length:"")};function s(){var s=this;this.emit=function(e,t){if(s){var n;if(null!=(i=s.eventsList[e])&&i.length)for(var i=i.slice(),o=0;o<i.length;o++){n=i[o];try{var r=n.callback.apply(s,[t]);if(1===n.type&&s.remove(e,n.callback),!1===r)break}catch(e){throw e}}return s}},this.eventsList={}}(ae=I=I||{})[ae.number=-1]="number",ae.string="";var g,m,c=["ret","retcode","code","errcode"],l=function(e){if("string"==typeof e)return e;try{return(JSON.stringify(e,(i=[],o=[],function(e,t){if(t instanceof Error)return"Error.message: "+t.message+" \\n  Error.stack: "+t.stack;if("object"==typeof t&&null!==t){var n=i.indexOf(t);if(-1!==n)return"[Circular "+o[n]+"]";i.push(t),o.push(e||"root")}return t}),4)||"undefined").replace(/"/gim,"")}catch(e){return"error happen when aegis stringify: \\n "+e.message+" \\n "+e.stack}var i,o};(j=g=g||{}).INFO_ALL="-1",j.API_RESPONSE="1",j.INFO="2",j.ERROR="4",j.PROMISE_ERROR="8",j.AJAX_ERROR="16",j.SCRIPT_ERROR="32",j.IMAGE_ERROR="64",j.CSS_ERROR="128",j.CONSOLE_ERROR="256",j.MEDIA_ERROR="512",j.RET_ERROR="1024",j.REPORT="2048",j.PV="4096",j.EVENT="8192",(I=P=P||{})[I.android=1]="android",I[I.ios=2]="ios",I[I.windows=3]="windows",I[I.macos=4]="macos",I[I.linux=5]="linux",I[I.devtools=6]="devtools",I[I.other=100]="other",(ae=H=H||{})[ae.unknown=100]="unknown",ae[ae.wifi=1]="wifi",ae[ae.net2g=2]="net2g",ae[ae.net3g=3]="net3g",ae[ae.net4g=4]="net4g",ae[ae.net5g=5]="net5g",ae[ae.net6g=6]="net6g",(j=m=m||{}).LOG="log",j.SPEED="speed",j.PERFORMANCE="performance",j.OFFLINE="offline",j.WHITE_LIST="whiteList",j.VITALS="vitals",j.PV="pv",j.CUSTOM_PV="customPV",j.EVENT="event",j.CUSTOM="custom",j.SDK_ERROR="sdkError";function y(e,n){var i,o=[],r=e.config;return e.lifeCycle.on("destroy",function(){o.length=0}),function(e,t){if(o.push(e),n&&o.length>=n)return o=O(o),t(o.splice(0,o.length)),void(i&&clearTimeout(i));i&&clearTimeout(i),i=setTimeout(function(){i=null,0<(o=O(o)).length&&t(o.splice(0,o.length))},r.delay)}}function v(e,t){return Array.isArray(e)?t(e.map(function(e){return a(a({},e),{msg:"string"==typeof e.msg?e.msg:[].concat(e.msg).map(l).join(" ")})})):t(a(a({},e),{msg:"string"==typeof e.msg?e.msg:l(e.msg)}))}function f(o){return function(e,t){o.lifeCycle.emit("modifyRequest",e);var n=o.config.modifyRequest;if("function"==typeof n)try{var i=n(e);"object"==typeof i&&"url"in i&&(e=i)}catch(e){console.error(e)}t(e)}}function p(i){return function(e,t){i.lifeCycle.emit("afterRequest",e);var n=i.config.afterRequest;"function"==typeof n&&!1===n(e)||t(e)}}function w(n){if(!n||!n.reduce||!n.length)throw new TypeError("createPipeline need at least one function param");return 1===n.length?function(e,t){n[0](e,t||T)}:n.reduce(function(n,i){return function(e,t){return void 0===t&&(t=T),n(e,function(e){return null==i?void 0:i(e,t)})}})}function d(t,n){Object.getOwnPropertyNames(t).forEach(function(e){"function"==typeof t[e]&&"constructor"!==e&&(n?n[e]="sendPipeline"===e?function(){return function(){}}:function(){}:t[e]=function(){})})}function x(){return void 0!==window.performance&&"function"==typeof performance.clearResourceTimings&&"function"==typeof performance.getEntriesByType&&"function"==typeof performance.now}var b,E,O=function(e){return e.filter(function(n,i){return"static"!==n.type||!e.find(function(e,t){return i!==t&&n.url===e.url&&200===n.status})})},R=function(e){e.level===g.INFO_ALL&&(e.level=g.INFO)},S=function(i){return function(e){return i.sendPipeline([function(e,n){return n({url:i.config.url||"",data:r(e),method:"post",contentType:"application/x-www-form-urlencoded",type:m.LOG,log:e,success:function(){var t=i.config.onReport;"function"==typeof t&&e.forEach(function(e){t(e)}),"function"==typeof n&&n([])},fail:function(e){"403 forbidden"===e&&i.destroy()}})}],m.LOG)(e)}},T=function(){},P=(Object.defineProperty(N.prototype,"__version__",{get:function(){return console.warn("__version__ has discard, please use version"),"1.24.45"},enumerable:!1,configurable:!0}),Object.defineProperty(N.prototype,"LogType",{get:function(){return console.warn("LogType has discard, please use logType"),g},enumerable:!1,configurable:!0}),N.prototype.init=function(e){this.setConfig(e);for(var t=0;t<N.installedPlugins.length;t++)try{N.installedPlugins[t].patch(this)}catch(e){this.sendSDKError(e)}this.lifeCycle.emit("onInited")},N.prototype.setConfig=function(e){Object.assign(this.config,e);var t=this.config,n=t.id,i=t.uin,o=t.version,r=t.ext1,s=t.ext2,a=t.ext3,e=t.aid,t=this.bean.id!==n||this.bean.uin!==i||this.bean.aid!==e;return this.bean.id=n||"",this.bean.uin=i||"",this.bean.version=o||"1.24.45",this.bean.aid=e||"",r&&(this.bean.ext1=encodeURIComponent(r)),s&&(this.bean.ext2=encodeURIComponent(s)),a&&(this.bean.ext3=encodeURIComponent(a)),t&&this.lifeCycle.emit("onConfigChange",this.config),this.config},N.use=function(e){-1===N.installedPlugins.indexOf(e)&&e.aegisPlugin&&N.installedPlugins.push(e)},N.unuse=function(e){e=N.installedPlugins.indexOf(e);-1!==e&&N.installedPlugins.splice(e,1)},N.prototype.info=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];var n={level:g.INFO,msg:e};1===e.length&&e[0].msg&&Object.assign(n,a({},e[0]),{level:g.INFO}),this.normalLogPipeline(n)},N.prototype.infoAll=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];var n={level:g.INFO_ALL,msg:e};1===e.length&&e[0].msg&&Object.assign(n,a({},e[0]),{level:g.INFO_ALL}),this.normalLogPipeline(n)},N.prototype.report=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];var n={level:g.REPORT,msg:e};1===e.length&&e[0].msg&&Object.assign(n,a({},e[0])),this.normalLogPipeline(n)},N.prototype.error=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];var n={level:g.ERROR,msg:e};1===e.length&&e[0].msg&&Object.assign(n,a({},e[0]),{level:g.ERROR}),this.normalLogPipeline(n)},N.prototype.speedLogPipeline=function(e){throw new Error(\'You need to override "speedLogPipeline" method\')},N.prototype.reportPv=function(n){var i,o=this;n&&(console.warn("reportPv is deprecated, please use reportEvent"),i=""+Object.getOwnPropertyNames(this.bean).filter(function(e){return"id"!==e}).map(function(e){return e+"="+o.bean[e]}).join("&"),this.sendPipeline([function(e,t){t({url:o.config.url+"/"+n+"?"+i,addBean:!1,type:m.CUSTOM_PV,fail:function(e){"403 forbidden"===e&&o.destroy()}})}],m.CUSTOM_PV)(null))},N.prototype.reportEvent=function(e){e&&((e="string"==typeof e?{name:e,ext1:this.config.ext1||"",ext2:this.config.ext2||"",ext3:this.config.ext3||""}:e).name?this.eventPipeline(e):console.warn("reportEvent params error"))},N.prototype.reportTime=function(e,t){if("object"==typeof e)return this.reportT(e);"string"==typeof e?"number"==typeof t?t<0||6e4<t?console.warn("reportTime: duration must between 0 and 60000"):this.submitCustomTime(e,t):console.warn("reportTime: second param must be number"):console.warn("reportTime: first param must be a string")},N.prototype.reportT=function(e){var t=e.name,n=e.duration,i=e.ext1,o=void 0===i?"":i,r=e.ext2,i=void 0===r?"":r,r=e.ext3,r=void 0===r?"":r,e=e.from;if("string"==typeof t&&"number"==typeof n&&"string"==typeof o&&"string"==typeof i&&"string"==typeof r){if(!(n<0||6e4<n))return this.submitCustomTime(t,n,o,i,r,void 0===e?"":e);console.warn("reportTime: duration must between 0 and 60000")}else console.warn("reportTime: params error")},N.prototype.time=function(e){"string"==typeof e?this.timeMap[e]?console.warn("Timer "+e+" already exists"):this.timeMap[e]=Date.now():console.warn("time: first param must be a string")},N.prototype.timeEnd=function(e){"string"==typeof e?this.timeMap[e]?(this.submitCustomTime(e,Date.now()-this.timeMap[e]),delete this.timeMap[e]):console.warn("Timer "+e+" does not exist"):console.warn("timeEnd: first param must be a string")},N.prototype.submitCustomTime=function(e,t,n,i,o,r){this.customTimePipeline({name:e,duration:t,ext1:n||this.config.ext1,ext2:i||this.config.ext2,ext3:o||this.config.ext3,from:r||void 0})},N.prototype.extendBean=function(e,t){this.bean[e]=t},N.prototype.sendPipeline=function(e,t){var n,r,s,a=this;return w(u([function(e,t){if("number"!=typeof n.config.random&&(console.warn("random must in [0, 1], default is 1."),n.config.random=1),!n.isHidden||!n.isGetSample)if(n.isGetSample)n.isHidden||t(e);else{if(n.isGetSample=!0,Math.random()<n.config.random)return n.isHidden=!1,t(e);n.isHidden=!0}},(s=t,function(e,t){var n=Array.isArray(e),i=n?e:[e];r.lifeCycle.emit("beforeRequest",e);var o=r.config.beforeRequest;(i="function"==typeof o?i.map(function(t){try{var e=o({logs:t,logType:s});return(null==e?void 0:e.logType)===s&&null!=e&&e.logs?e.logs:!1!==e&&t}catch(e){return t}}).filter(function(e){return!1!==e}):i).length&&t(n?i:i[0])})],e,[f(r=n=this),function(i,o){a.request(i,function(){for(var e,t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];o({isErr:!1,result:t,logType:null==i?void 0:i.type,logs:null==i?void 0:i.log}),null===(e=null==i?void 0:i.success)||void 0===e||e.call.apply(e,u([i],t))},function(){for(var e,t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];o({isErr:!0,result:t,logType:null==i?void 0:i.type,logs:null==i?void 0:i.log}),null===(e=null==i?void 0:i.fail)||void 0===e||e.call.apply(e,u([i],t))})},p(this)]))},N.prototype.send=function(e,o,r){var t=this;return w([f(this),function(n,i){t.request(n,function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];i({isErr:!1,result:e,logType:n.type,logs:n.log}),null==o||o.apply(void 0,e)},function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];i({isErr:!0,result:e,logType:n.type,logs:n.log}),null==r||r.apply(void 0,e)})},p(this)])(e)},N.prototype.request=function(e,t,n){throw new Error(\'You need to override "request" method\')},N.prototype.sendSDKError=function(e){var n=this;this.sendPipeline([function(e,t){t({url:n.config.url+"?id=1085&msg[0]="+encodeURIComponent(l(e))+"&level[0]=2&from="+n.config.id+"&count=1&version="+n.config.id+"(1.24.45)",addBean:!1,method:"get",type:m.SDK_ERROR,log:e})}],m.SDK_ERROR)(e)},N.prototype.destroy=function(e){void 0===e&&(e=!1);var t,n,i=N.instances.indexOf(this);-1!==i&&N.instances.splice(i,1);for(var o=N.installedPlugins.length-1;0<=o;o--)try{N.installedPlugins[o].unpatch(this)}catch(e){this.sendSDKError(e)}if(this.lifeCycle.emit("destroy"),this.lifeCycle.clear(),e)t=this,n=Object.getOwnPropertyDescriptors(t),Object.keys(n).forEach(function(e){n[e].writable&&(t[e]=null)}),Object.setPrototypeOf(this,null);else{for(var r=this;r.constructor!==Object&&d(r,this),r=Object.getPrototypeOf(r););0===N.instances.length&&(e=Object.getPrototypeOf(this).constructor,d(e),d(N))}},N.version="1.24.45",N.instances=[],N.logType=g,N.installedPlugins=[],N),C=(A.prototype.patch=function(e){this.canUse(e)&&this.exist(e)&&(this.instances.push(e),this.triggerInit(e),this.triggerOnNewAegis(e))},A.prototype.unpatch=function(e){e=this.instances.indexOf(e);-1!==e&&this.instances.splice(e,1)},A.prototype.countInstance=function(){return this.instances.length},A.prototype.uninstall=function(){var e;null===(e=null===(e=this.option)||void 0===e?void 0:e.destroy)||void 0===e||e.apply(this)},A.prototype.walk=function(n){var i=this;this.instances.forEach(function(e){var t=i.canUse(e);t&&n(e,t)})},A.prototype.canUse=function(e){e=this.getConfig(e);return!(!e||"object"!=typeof e)||!!e},A.prototype.getConfig=function(e){return null===(e=e.config)||void 0===e?void 0:e[this.name]},A.prototype.exist=function(e){return-1===this.instances.indexOf(e)},A.prototype.triggerInit=function(e){var t;this.inited||(this.inited=!0,null===(t=null===(t=this.option)||void 0===t?void 0:t.init)||void 0===t||t.call(this.option,this.getConfig(e)))},A.prototype.triggerOnNewAegis=function(e){var t;null===(t=null===(t=this.option)||void 0===t?void 0:t.onNewAegis)||void 0===t||t.call(this.option,e,this.getConfig(e))},A),I=new C({name:"aid",aid:"",init:function(e){try{var t=!0!==e&&e||window.localStorage.getItem("AEGIS_ID");t||(t="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(e){var t=16*Math.random()|0;return("x"===e?t:3&t|8).toString(16)}),window.localStorage.setItem("AEGIS_ID",t)),this.aid=t}catch(e){}},onNewAegis:function(e){e.bean.aid=this.aid,e.config.aid=this.aid}}),L=function(t){if(t.payload){var n={};return Object.keys(t).forEach(function(e){"payload"!==e&&(n[e]=t[e])}),n}return t};function A(e){this.aegisPlugin=!0,this.name="",this.instances=[],this.inited=!1,e.$walk=this.walk.bind(this),e.$getConfig=this.getConfig.bind(this),this.option=e,this.name=e.name}function N(e){var i,t,s,n,a,u,o,c,r,l,f,p,d=this;this.isGetSample=!1,this.isHidden=!1,this.config={version:0,delay:1e3,onError:!0,repeat:5,random:1,aid:!0,device:!0,pagePerformance:!0,webVitals:!0,speedSample:!0,hostUrl:"https://aegis.qq.com",url:"",offlineUrl:"",whiteListUrl:"",pvUrl:"",speedUrl:"",customTimeUrl:"",performanceUrl:"",webVitalsUrl:"",eventUrl:""},this.isWhiteList=!1,this.lifeCycle=new h,this.bean={},this.normalLogPipeline=w([y(this,5),v,(f=this.config,p={},function(e,t){var n="number"==typeof f.repeat?f.repeat:5;if(0===n)return t(e);t(e.filter(function(e){return e.level!==g.ERROR&&e.level!==g.PROMISE_ERROR&&e.level!==g.AJAX_ERROR&&e.level!==g.SCRIPT_ERROR&&e.level!==g.IMAGE_ERROR&&e.level!==g.CSS_ERROR&&e.level!==g.MEDIA_ERROR||(p[e.msg]=p[e.msg]||0,p[e.msg]+=1,!(p[e.msg]>n))}))}),(r=this.lifeCycle.emit,l=this.config,function(e,t){var n=l.logCreated;if("function"!=typeof n)return r("beforeWrite",e),t(e);e=e.filter(function(e){return!1!==n(e)});return r("beforeWrite",e),t(e)}),(i=this,setTimeout(function(){var e=i.config.pvUrl,n=void 0===e?"":e;n&&i.sendPipeline([function(e,t){t({url:n,type:m.PV,fail:function(e){"403 forbidden"===e&&i.destroy()}})}],m.PV)(null)},100),function(e,t){t(e)}),(o=u=a=!1,c=[],(s=this).lifeCycle.on("onConfigChange",function(){n&&clearTimeout(n),n=setTimeout(function(){var e,n;!o&&s.config&&(o=!0,e=s.config.whiteListUrl,(n=void 0===e?"":e)&&s.sendPipeline([function(e,t){t({url:n,type:m.WHITE_LIST,success:function(e){u=!0;try{var t=e.data||JSON.parse(e),n=t.retcode,i=t.result,o=void 0===i?{}:i;if(0===n){if(a=o.is_in_white_list,s.isWhiteList=a,o.shutdown)return void s.destroy();0<=o.rate&&o.rate<=1&&(s.config.random=o.rate,s.isGetSample=!1)}s.isWhiteList&&c.length?S(s)(c.splice(0),function(){}):!s.isWhiteList&&c.length&&(c.length=0);var r=s.config.onWhitelist;"function"==typeof r&&r(a)}catch(e){}},fail:function(e){"403 forbidden"===e&&s.destroy(),u=!0}})}],m.WHITE_LIST)(null),o=!1)},s.config.uin?50:500)}),s.lifeCycle.on("destroy",function(){c.length=0}),function(e,t){var n;a||null!==(n=null===(n=s.config)||void 0===n?void 0:n.api)&&void 0!==n&&n.reportRequst?t(e.concat(c.splice(0)).map(function(e){return R(e),e})):(e=e.filter(function(e){return e.level!==g.INFO&&e.level!==g.API_RESPONSE?(R(e),!0):(u||(c.push(e),200<=c.length&&(c.length=200)),!1)})).length&&t(e)}),function(e,t){var n=JSON.parse(JSON.stringify(e));d.lifeCycle.emit("beforeReport",n);var i=d.config.beforeReport;(e="function"==typeof i?e.filter(function(e){return!1!==i(e)}):e).length&&t(e)},S(this)]),this.eventPipeline=w([y(this,5),function(e){d.sendPipeline([function(e,t){var n=e.map(function(e){return{name:e.name,ext1:e.ext1||d.config.ext1||"",ext2:e.ext2||d.config.ext2||"",ext3:e.ext3||d.config.ext3||""}});t({url:d.config.eventUrl+"?payload="+encodeURIComponent(JSON.stringify(n)),type:m.EVENT,log:e,fail:function(e){"403 forbidden"===e&&d.destroy()}})}],m.EVENT)(e)}]),this.timeMap={},this.customTimePipeline=w([y(this,5),function(e){return d.sendPipeline([function(e,t){t({url:d.config.customTimeUrl+"?payload="+encodeURIComponent(JSON.stringify({custom:e})),type:m.CUSTOM,log:e,fail:function(e){"403 forbidden"===e&&d.destroy()}})}],m.CUSTOM)(e)}]),this.config=(t=this.config,void 0===(e=e.hostUrl)&&(e="https://aegis.qq.com"),t.url=t.url||e+"/collect",t.offlineUrl=t.offlineUrl||e+"/offline",t.whiteListUrl=t.whiteListUrl||e+"/collect/whitelist",t.pvUrl=t.pvUrl||e+"/collect/pv",t.eventUrl=t.eventUrl||e+"/collect/events",t.speedUrl=t.speedUrl||e+"/speed",t.customTimeUrl=t.customTimeUrl||e+"/speed/custom",t.performanceUrl=t.performanceUrl||e+"/speed/performance",t.webVitalsUrl=t.webVitalsUrl||e+"/speed/webvitals",t),N.instances.push(this)}new C({name:"reportAssetSpeed"}),new C({name:"reportApiSpeed"}),(H=b=b||{})[H.unknown=100]="unknown",H[H.wifi=1]="wifi",H[H.net2g=2]="net2g",H[H.net3g=3]="net3g",H[H.net4g=4]="net4g",H[H.net5g=5]="net5g",H[H.net6g=6]="net6g",(ae=E=E||{})[ae.android=1]="android",ae[ae.ios=2]="ios",ae[ae.windows=3]="windows",ae[ae.macos=4]="macos",ae[ae.linux=5]="linux",ae[ae.other=100]="other";var j=new C({name:"device",onNewAegis:function(e){e.extendBean("platform",this.getPlatform()),e.extendBean("netType",b.unknown),this.refreshNetworkTypeToBean(e)},getPlatform:function(){var t={android:/\\bAndroid\\s*([^;]+)/,ios:/\\b(iPad|iPhone|iPod)\\b.*? OS ([\\d_]+)/,windows:/\\b(Windows NT)/,macos:/\\b(Mac OS)/,linux:/\\b(Linux)/i},e=Object.keys(t).find(function(e){return t[e].test(navigator.userAgent)});return e?E[e]:E.other},refreshNetworkTypeToBean:function(t){var n=this,e=t.config;e&&("function"==typeof e.getNetworkType?e.getNetworkType:U)(function(e){b[e]||(e=b.unknown),t.extendBean("netType",e),n.NetworkRefreshTimer=setTimeout(function(){n.refreshNetworkTypeToBean(t),clearTimeout(n.NetworkRefreshTimer)},1e4)})}}),U=function(e){var t="",n=navigator.userAgent.match(/NetType\\/(\\w+)/);n?t=n[1]:navigator.connection&&(t=navigator.connection.effectiveType||navigator.connection.type),e((t=t=t||"unknown",0<=(t=String(t).toLowerCase()).indexOf("4g")?b.net4g:0<=t.indexOf("wifi")?b.wifi:0<=t.indexOf("5g")?b.net5g:0<=t.indexOf("6g")?b.net6g:0<=t.indexOf("3g")?b.net3g:0<=t.indexOf("2g")?b.net2g:b.unknown))};new C({name:"onError"}),new C({name:"pagePerformance"});var k,_,D,q,F,M,V,B,G,W=1,H=new C({name:"pagePerformance",onNewAegis:function(i){var o=this;if(x())if(k)this.publish(k,i);else try{this.getFirstScreenTiming(i,function(e){var t,n=performance.timing;n&&(t=n.loadEventStart-n.domInteractive,k={dnsLookup:n.domainLookupEnd-n.domainLookupStart,tcp:n.connectEnd-n.connectStart,ssl:0===n.secureConnectionStart?0:n.requestStart-n.secureConnectionStart,ttfb:n.responseStart-n.requestStart,contentDownload:n.responseEnd-n.responseStart,domParse:n.domInteractive-n.domLoading,resourceDownload:t=t<0?1070:t,firstScreenTiming:Math.floor(e)},o.publish(k,i))})}catch(i){}},publish:function(e,s){var a=this;s.sendPipeline([function(e,t){var n,i=[];for(n in e)i.push(n+"="+e[n]);var o=a.$getConfig(s);if(o){var r=-1===s.config.performanceUrl.indexOf("?")?"?":"&";return"function"==typeof o.urlHandler?t({url:s.config.performanceUrl+r+i.join("&")+"&from="+(encodeURIComponent(o.urlHandler())||window.location.href),beanFilter:["from"],type:m.PERFORMANCE,log:e}):t({url:s.config.performanceUrl+r+i.join("&"),type:m.PERFORMANCE,log:e})}}],m.PERFORMANCE)(e)},getFirstScreenTiming:function(o,t){o.lifeCycle.on("destroy",function(){l&&clearTimeout(l)});var n=["script","style","link","br"],r=[],s=this,a={},u=new MutationObserver(function(e){var t={roots:[],rootsDomNum:[],time:performance.now()};e.forEach(function(e){e&&e.addedNodes&&e.addedNodes.forEach&&e.addedNodes.forEach(function(e){1===e.nodeType&&(e.hasAttribute("AEGIS-FIRST-SCREEN-TIMING")||e.querySelector("[AEGIS-FIRST-SCREEN-TIMING]"))?(Object.prototype.hasOwnProperty.apply(a,[t.time])||(a[t.time]=[]),a[t.time].push(e)):1!==e.nodeType||-1!==n.indexOf(e.nodeName.toLocaleLowerCase())||s.isEleInArray(e,t.roots)||e.hasAttribute("AEGIS-IGNORE-FIRST-SCREEN-TIMING")||(t.roots.push(e),t.rootsDomNum.push(s.walkAndCount(e)||0))})}),t.roots.length&&r.push(t)});u.observe(document,{childList:!0,subtree:!0});var c=function(){var n=0,i=0,e=Object.keys(a).filter(function(e){return a[+e].find(function(e){return s.isInFirstScreen(e)})});e.length?(i=Math.max.apply(null,e),o.firstScreenInfo={element:null===(e=a[i])||void 0===e?void 0:e[0],timing:i,markDoms:a}):r.forEach(function(e){for(var t=0;t<e.roots.length;t++)e.rootsDomNum[t]>n&&s.isInFirstScreen(e.roots[t])&&(n=e.rootsDomNum[t],i=e.time,o.firstScreenInfo={element:e.roots[t],timing:i})}),!i&&W?l=setTimeout(function(){return c()},3e3):(u.disconnect(),null==t||t(i)),--W},l=setTimeout(function(){return c()},3e3)},isEleInArray:function(e,t){return!(!e||e===document.documentElement)&&(-1!==t.indexOf(e)||this.isEleInArray(e.parentElement,t))},isInFirstScreen:function(e){if(!e||"function"!=typeof e.getBoundingClientRect)return!1;var t=e.getBoundingClientRect(),n=window.innerHeight,e=window.innerWidth;return 0<=t.left&&t.left<e&&0<=t.top&&t.top<n&&0<t.width&&0<t.height},walkAndCount:function(e){var t=0;if(e&&1===e.nodeType){t+=1;var n=e.children;if(null!=n&&n.length)for(var i=0;i<n.length;i++)t+=this.walkAndCount(n[i])}return t}});function J(){F=[],D=-1,_=null,X(addEventListener)}function K(e,t){_||(_=t,D=e,q=new Date,X(removeEventListener),Q())}function Q(){var t;0<=D&&D<q-V&&(t={entryType:"first-input",name:_.type,target:_.target,cancelable:_.cancelable,startTime:_.timeStamp,processingStart:_.timeStamp+D},F.forEach(function(e){e(t)}),F=[])}function X(t){["mousedown","keydown","touchstart","pointerdown"].forEach(function(e){return t(e,B,M)})}M={passive:!0,capture:!0},V=new Date,B=function(e){var t,n,i,o;function r(){K(n,i),o()}function s(){o()}e.cancelable&&(t=(1e12<e.timeStamp?new Date:performance.now())-e.timeStamp,"pointerdown"==e.type?(n=t,i=e,o=function(){removeEventListener("pointerup",r,M),removeEventListener("pointercancel",s,M)},addEventListener("pointerup",r,M),addEventListener("pointercancel",s,M)):K(t,e))},G="hidden"===document.visibilityState?0:1/0,addEventListener("visibilitychange",function e(t){"hidden"===document.visibilityState&&(G=t.timeStamp,removeEventListener("visibilitychange",e,!0))},!0),J(),self.webVitals={firstInputPolyfill:function(e){F.push(e),Q()},resetFirstInputPolyfill:J,get firstHiddenTime(){return G}};function $(e,t){return{name:e,value:void 0===t?-1:t,delta:0,entries:[],id:"v1-".concat(Date.now(),"-").concat(Math.floor(8999999999999*Math.random())+1e12)}}function Y(e,t){try{if(PerformanceObserver.supportedEntryTypes.includes(e)){if("first-input"===e&&!("PerformanceEventTiming"in self))return;var n=new PerformanceObserver(function(e){return e.getEntries().map(t)});return n.observe({type:e,buffered:!0}),n}}catch(e){}}function z(t){addEventListener("pageshow",function(e){e.persisted&&t(e)},!0)}function Z(e,t,n){var i;return function(){0<=t.value&&(n||re.has(t)||"hidden"===document.visibilityState)&&(t.delta=t.value-(i||0),!t.delta&&void 0!==i||(i=t.value,e(t)))}}function ee(){oe(function(e){e=e.timeStamp;se=e},!0)}function te(){return se<0&&((se=self.webVitals.firstHiddenTime)===1/0&&ee(),z(function(){setTimeout(function(){se="hidden"===document.visibilityState?0:1/0,ee()},0)})),{get timeStamp(){return se}}}var ne,ie,oe=function(t,n){function i(e){"pagehide"!==e.type&&"hidden"!==document.visibilityState||(t(e),n&&(removeEventListener("visibilitychange",i,!0),removeEventListener("pagehide",i,!0)))}addEventListener("visibilitychange",i,!0),addEventListener("pagehide",i,!0)},re=new("function"==typeof WeakSet?WeakSet:Set),se=-1,ae=(new C({name:"webVitals"}),new C({name:"webVitals",onNewAegis:function(e){if(x()&&"function"==typeof window.PerformanceObserver)try{y=this.publish.bind(this,e),w=te(),b=$("FCP"),E=performance.getEntriesByName("first-contentful-paint")[0],O=E?null:Y("paint",T),(E||O)&&(v=Z(y,b,void 0),E&&T(E),z(function(e){b=$("FCP"),v=Z(y,b,void 0),requestAnimationFrame(function(){requestAnimationFrame(function(){b.value=performance.now()-e.timeStamp,re.add(b),v()})})})),f=this.publish.bind(this,e),h=te(),g=$("LCP"),(m=Y("largest-contentful-paint",S))&&(p=Z(f,g,void 0),d=function(){re.has(g)||(m.takeRecords().map(S),m.disconnect(),re.add(g),p())},["keydown","click"].forEach(function(e){addEventListener(e,d,{once:!0,capture:!0})}),oe(d,!0),z(function(e){g=$("LCP"),p=Z(f,g,void 0),requestAnimationFrame(function(){requestAnimationFrame(function(){g.value=performance.now()-e.timeStamp,re.add(g),p()})})})),s=this.publish.bind(this,e),a=te(),u=$("FID"),c=Y("first-input",R),l=Z(s,u,void 0),c&&oe(function(){c.takeRecords().map(R),c.disconnect()},!0),c||window.webVitals.firstInputPolyfill(R),z(function(){u=$("FID"),l=Z(s,u,void 0),window.webVitals.resetFirstInputPolyfill(),window.webVitals.firstInputPolyfill(R)}),n=this.publish.bind(this,e),o=$("CLS",0),(r=Y("layout-shift",t))&&(i=Z(n,o,void 0),oe(function(){r.takeRecords().map(t),i()}),z(function(){o=$("CLS",0),i=Z(n,o,void 0)}))}catch(e){}function t(e){e.hadRecentInput||(o.value+=e.value,o.entries.push(e),i())}var n,i,o,r,s,a,u,c,l,f,p,d,h,g,m,y,v,w,b,E,O;function R(e){e.startTime<a.timeStamp&&(u.value=e.processingStart-e.startTime,u.entries.push(e),re.add(u),l())}function S(e){var t=e.startTime;t<h.timeStamp&&(g.value=t,g.entries.push(e)),p()}function T(e){"first-contentful-paint"===e.name&&(O&&O.disconnect(),e.startTime<w.timeStamp&&(b.value=e.startTime,b.entries.push(e),re.add(b),v()))}},publish:function(u,e){u.sendPipeline([function(e,t){var n,i={FCP:-1,LCP:-1,FID:-1,CLS:-1},o=e.name,r=e.value,s=[];for(n in i)s.push(n+"="+(n===o?r:i[n]));var a=-1===u.config.performanceUrl.indexOf("?")?"?":"&";setTimeout(function(){t({url:u.config.webVitalsUrl+a+s.join("&"),type:m.VITALS,log:e})})}],m.VITALS)(e)},destroy:function(){this.option.publish=function(){}}})),ue=0,P=(i(ie=ce,P=ne=P),ie.prototype=null===P?Object.create(P):(le.prototype=P.prototype,new le),ce.prototype.getBean=function(t){var n=this;return void 0===t&&(t=[]),""+Object.getOwnPropertyNames(this.bean).filter(function(e){return-1===t.indexOf(e)}).map(function(e){return e+"="+n.bean[e]}).join("&")},ce.prototype.request=function(e,t,n){var i=this;if(e&&"string"==typeof e.url&&""!==e.url&&this.bean.id)if(this.requesting)this.requestQueue.push({options:e,success:t,fail:n});else{this.requesting=!0;var o=e.url;!1!==e.addBean&&(o=o+(-1===o.indexOf("?")?"?":"&")+this.getBean(e.beanFilter)),e.url=o;var r=e.method||"get",o=this.config.onBeforeRequest;if(!(e=o?o(e,this):e))return console.log("Sending request blocked");if(!e.url)return console.warn("Please handle the parameters reasonably, options.url is necessary");var s=new XMLHttpRequest;s.sendByAegis=!0,s.addEventListener("readystatechange",function(){var e;4===s.readyState&&(i.requesting=!1,i.requestQueue&&((e=i.requestQueue.shift())&&i.request(e.options,e.success,e.fail),400<=s.status||0===s.status?null==n||n(s.response):null==t||t(s.response)))}),"get"===r.toLocaleLowerCase()?(s.open("get",function(e,n){if("string"!=typeof e)return"";if("object"==typeof n&&n){var t=Object.getOwnPropertyNames(n).map(function(e){var t=n[e];return e+"="+("string"==typeof t?encodeURIComponent(t):encodeURIComponent(JSON.stringify(t)))}).join("&").replace(/eval/gi,"evaI");return e+(-1===e.indexOf("?")?"?":"&")+t}return e}(e.url,e.data)),s.send()):(s.open("post",e.url),e.contentType&&s.setRequestHeader("Content-Type",e.contentType),"string"==typeof e.data&&(e.data=e.data.replace(/eval/gi,"evaI")),s.send(e.data))}},ce.useAsyncPlugin=function(s,e){var t=(e=void 0===e?{}:e).exportsConstructor,a=void 0===t?"aegis-plugin-"+ue:t,t=e.onAegisInit,u=void 0===t?function(){}:t,e=e.onAegisInitAndPluginLoaded,c=void 0===e?function(){}:e;if(ue+=1,"string"!=typeof s)throw new TypeError("useAsyncPlugin first param must be string");if("function"!=typeof u||"function"!=typeof c)throw new TypeError("onAegisInit and onAegisInitAndPluginLoaded must be function");this.use(new C({name:"asyncPlugin",onNewAegis:function(t){try{u(t),ce.asyncPlugin[s]?c(t,window[ce.asyncPlugin[s]]):(e=s,n=a,i=function(e){e||(ce.asyncPlugin[s]=a,e=window[a],c(t,e))},o=document.createElement("script"),r=document.head,"function"==typeof n&&(i=n,n=""),o.src=e,o.setAttribute("name",n),o.name=n,o.setAttribute("crossorigin","anonymous"),o.crossorigin="anonymous",o.defer=!0,o.hasLoaded=!1,o.onreadystatechange=function(){o.hasLoaded||o.readyState&&"loaded"!==o.readyState&&"complete"!==o.readyState||(o.hasLoaded=!0,"function"==typeof i&&i(!1),setTimeout(function(){r.contains(o)&&r.removeChild(o)}))},o.onload=o.onreadystatechange,o.onerror=function(){"function"==typeof i&&i(!0),setTimeout(function(){r.contains(o)&&r.removeChild(o)})},"complete"===document.readyState?r.appendChild(o):window.addEventListener("load",function(){r.appendChild(o)}))}catch(e){console.log("error on below is caused by "+s+" "),console.error(e)}var e,n,i,o,r}}))},ce.prototype.uploadLogs=function(e,t){this.lifeCycle.emit("uploadLogs",e=void 0===e?{}:e,t=void 0===t?{}:t)},ce.sessionID="session-"+Date.now(),ce.asyncPlugin={},ce.urls={aegisCollect:"https://aegis.qq.com/collect",flog:"https://cdn-go.cn/vasdev/web_webpersistance_v2/v1.8.2/flog.core.min.js",shadowLog:""},ce);function ce(e){var t,n,i,o,r,s=ne.call(this,e)||this;s.requestQueue=[],s.requesting=!1,s.speedLogPipeline=w([(o=s.config,r={},function(e,t){var n,i;o.speedSample?(i="object"==typeof o.repeat?o.repeat:{repeat:o.repeat},n=+i.speed||+i.repeat||5,Array.isArray(e)?(i=e.filter(function(e){var t=!r[e.url]||r[e.url]<n;return r[e.url]=1+~~r[e.url],t})).length&&t(i):(!r[e.url]||r[e.url]<n)&&(r[e.url]=1+~~r[e.url],t(e))):t(e)}),y(s),(i=s,function(t,n){U(function(e){i.extendBean("netType",e),n(t)})}),function(e,t){s.lifeCycle.emit("beforeReportSpeed",e);var o=s.config.beforeReportSpeed;if((e="function"==typeof o?e.filter(function(e){var t,n,i=!1!==o(e);return"fetch"===e.type&&void 0===e.ret&&e.payload&&(t=(n=function(e,t,n){var i;try{if("function"==typeof(null==t?void 0:t.retCodeHandler)){var o=t.retCodeHandler(e,null==n?void 0:n.url,null==n?void 0:n.ctx)||{};return{code:void 0===(r=o.code)?"unknown":r,isErr:o.isErr}}"string"==typeof e&&(e=JSON.parse(e)),"function"==typeof(null===(i=null==t?void 0:t.ret)||void 0===i?void 0:i.join)&&(c=[].concat(t.ret.map(function(e){return e.toLowerCase()})));var r,s=Object.getOwnPropertyNames(e).filter(function(e){return-1!==c.indexOf(e.toLowerCase())});return s.length?{code:""+(r=Number(e[s[0]])),isErr:0!==r}:{code:"unknown",isErr:!1}}catch(e){return{code:"unknown",isErr:!1}}}(e.payload.data.response,s.config.api,{url:e.payload.data.url,ctx:e.payload.data})||{}).code,n=n.isErr,e.ret=t,e.isErr=+n),i}):e).length)return t(e)},function(e){return s.sendPipeline([function(e,t){var n,i,o,r;t({type:m.SPEED,url:""+s.config.speedUrl,method:"post",data:(n=e,i=s.bean,o={fetch:[],static:[]},r=new FormData,Array.isArray(n)?n.forEach(function(e){var t=L(e);o[e.type].push(t)}):(t=L(n),o[n.type].push(t)),r.append("payload",JSON.stringify(a({duration:o},i))),r),log:e})}],m.SPEED)(e)}]),e.asyncPlugin=!0;try{"undefined"!=typeof document&&(e.uin=e.uin||(null!==(t=document.cookie.match(/\\buin=\\D+(\\d*)/))&&void 0!==t?t:[])[1]||(null!==(n=document.cookie.match(/\\bilive_uin=\\D*(\\d+)/))&&void 0!==n?n:[])[1]||""),s.init(e),s.extendBean("sessionId",ce.sessionID),s.extendBean("from",encodeURIComponent(e.pageUrl||location.href)),"undefined"!=typeof document&&s.extendBean("referer",encodeURIComponent(document.referrer||"")),e.ext1&&s.extendBean("ext1",e.ext1),e.ext2&&s.extendBean("ext2",e.ext2),e.ext3&&s.extendBean("ext3",e.ext3)}catch(e){console.warn(e),console.log("%cThe above error occurred in the process of initializing Aegis, which will affect your normal use of Aegis.\\nIt is recommended that you contact us for feedback and thank you for your support.","color: red"),s.sendSDKError(e)}return s}function le(){this.constructor=ie}return new C({name:"offlineLog"}),new C({name:"spa"}),new C({name:"ie"}),P.use(H),P.use(ae),P.use(I),P.use(j),P});\n(function(window) {\n  if(!window.Aegis) return;\n  window.aegis = new window.Aegis({\n      id: \'uAkJVYKdgqmAnzaxOU\',\n      spa: true,\n      beforeReport(log) {\n          log = log || {};\n          var level = log.level;\n          var msg = log.msg;\n          var isNotReport = level === \'4\' && msg && /(?:insertStyle|processRandomSelector|Script error|bannerNight|runCustomize)/ig.test(msg);\n          return !isNotReport ? log : false;\n      }\n  });\n})(window);\n</script><link rel="preload" href="//dlweb.sogoucdn.com/vs/static/js/vsbundle.3ed018b1.js" as="script" crossorigin="anonymous"><link rel="preload" href="//search.sogoucdn.com/hanyu/pc/static/js/vendors.8629acfd.js" as="script"><link rel="preload" href="//search.sogoucdn.com/hanyu/pc/static/css/app.3f40f372.css" as="style"><link rel="preload" href="//search.sogoucdn.com/hanyu/pc/static/js/app.1a08a115.js" as="script"><link rel="preload" href="//search.sogoucdn.com/hanyu/pc/static/js/index.128f2cfb.js" as="script"><link rel="prefetch" href="//search.sogoucdn.com/hanyu/pc/static/css/1.f8dd8ab0.css"><link rel="prefetch" href="//search.sogoucdn.com/hanyu/pc/static/css/4.b6af2be8.css"><link rel="prefetch" href="//search.sogoucdn.com/hanyu/pc/static/js/4.d19b6161.js"><link rel="prefetch" href="//search.sogoucdn.com/hanyu/pc/static/js/detail.1b2793cb.js"><link rel="stylesheet" href="//search.sogoucdn.com/hanyu/pc/static/css/app.3f40f372.css"></head><body><div id="app"> <div id="pageApp" data-server-rendered="true"><div class="wrap"><div class="header-index"><div class="top-nav"><ul><li data-uigs="pbtype:cl;uigsmid:page;uigd_mids:page;uigs_id:pageclk"><a href="//www.sogou.com/web?ie=utf8" data-type="page" data-key="query" target="_blank">\xe7\xbd\x91\xe9\xa1\xb5</a></li><li data-uigs="pbtype:cl;uigsmid:wechat;uigd_mids:wechat;uigs_id:wechatclk"><a href="//weixin.sogou.com/weixin?ie=utf8" data-type="wechat" data-key="query" target="_blank">\xe5\xbe\xae\xe4\xbf\xa1</a></li><li data-uigs="pbtype:cl;uigsmid:zhihu;uigd_mids:zhihu;uigs_id:zhihuclk"><a href="//zhihu.sogou.com/zhihu?ie=utf8&amp;p=73351201" data-type="zhihu" data-key="query" target="_blank">\xe7\x9f\xa5\xe4\xb9\x8e</a></li><li data-uigs="pbtype:cl;uigsmid:pic;uigd_mids:pic;uigs_id:picclk"><a href="//pic.sogou.com/pics?ie=utf8&amp;p=40230504" data-type="pic" data-key="query" target="_blank">\xe5\x9b\xbe\xe7\x89\x87</a></li><li data-uigs="pbtype:cl;uigsmid:video;uigd_mids:video;uigs_id:videoclk"><a href="//v.sogou.com/v?ie=utf8&amp;p=40230608" data-type="video" data-key="query" target="_blank">\xe8\xa7\x86\xe9\xa2\x91</a></li><li data-uigs="pbtype:cl;uigsmid:mingyi;uigd_mids:mingyi;uigs_id:mingyiclk"><a href="//www.sogou.com/web?m2web=mingyi.sogou.com&amp;ie=utf8" data-type="mingyi" data-key="keyword" target="_blank">\xe5\x8c\xbb\xe7\x96\x97</a></li><li data-uigs="pbtype:cl;uigsmid:hanyu;uigd_mids:hanyu;uigs_id:hanyuclk" class="cur"><span>\xe6\xb1\x89\xe8\xaf\xad</span></li><li data-uigs="pbtype:cl;uigsmid:fanyi;uigd_mids:fanyi;uigs_id:fanyiclk"><a href="//fanyi.sogou.com/?fr=common_index_nav_pc_result&amp;ie=utf8" data-type="fanyi" data-key="keyword" target="_blank">\xe7\xbf\xbb\xe8\xaf\x91</a></li><li data-uigs="pbtype:cl;uigsmid:wenwen;uigd_mids:wenwen;uigs_id:wenwenclk"><a href="//wenwen.sogou.com/?ie=utf8&amp;ch=hanyusearch" data-type="wenwen" data-key="w" target="_blank">\xe9\x97\xae\xe9\x97\xae</a></li><li data-uigs="pbtype:cl;uigsmid:baike;uigd_mids:baike;uigs_id:baikeclk"><a href="//baike.sogou.com/" data-type="baike" data-key="query" target="_blank">\xe7\x99\xbe\xe7\xa7\x91</a></li> <li class="show-more"><a href="javascript:void(0);">\n          \xe6\x9b\xb4\xe5\xa4\x9a\n          <i class="m-arr"></i></a> <div class="pos-more" style="display:none;"><!----><a href="//map.sogou.com/?ie=utf8" data-type="map" data-key="#lq" target="_blank" data-uigs="pbtype:cl;uigsmid:map;uigd_mids:map;uigs_id:mapclk">\xe5\x9c\xb0\xe5\x9b\xbe</a><a href="//zhishi.sogou.com" data-type="zhishi" data-key="query" target="_blank" data-uigs="pbtype:cl;uigsmid:zhishi;uigd_mids:zhishi;uigs_id:zhishiclk">\xe7\x9f\xa5\xe8\xaf\x86</a><a href="//as.sogou.com/so?ie=utf8" data-type="as" data-key="query" target="_blank" data-uigs="pbtype:cl;uigsmid:as;uigd_mids:as;uigs_id:asclk">\xe5\xba\x94\xe7\x94\xa8</a><span class="all"><a href="//www.sogou.com/docs/more.htm?ie=utf8" target="_blank">\xe5\x85\xa8\xe9\x83\xa8</a></span></div></li></ul></div> <!----></div> <div class="content-index"><div class="logo-index"><span></span></div></div> <div class="search-box"><span data-uigs="pbtype:cl;uigsmid:searchbox;uigd_mids:searchbox;uigs_id:searchboxclk" class="sec-input-box"><input type="text" name="query" maxlength="100" placeholder="\xe8\xaf\xb7\xe8\xbe\x93\xe5\x85\xa5\xe8\xa6\x81\xe6\x9f\xa5\xe8\xaf\xa2\xe7\x9a\x84\xe5\x8d\x95\xe5\xad\x97\xe3\x80\x81\xe8\xaf\x8d\xe8\xaf\xad\xe3\x80\x81\xe6\x88\x90\xe8\xaf\xad" len="80" autocomplete="off" value="" class="sec-input"> <!----></span> <span class="enter-input"><input type="submit" value="\xe6\x90\x9c\xe7\x8b\x97\xe6\x90\x9c\xe7\xb4\xa2"></span> <div class="suggestion nobg" style="display:none;"><div class="suginner"><ul class="suglist"></ul></div> <div class="sug-history" style="display: block;"><!----></div></div></div> <!----> <div class="footer-hanyu fixed"><a href="http://fuwu.sogou.com/" target="_blank">\xe4\xbc\x81\xe4\xb8\x9a\xe6\x8e\xa8\xe5\xb9\xbf</a> -\n  <a href="https://www.sogou.com/docs/terms.htm?v=1" target="_blank">\xe5\x85\x8d\xe8\xb4\xa3\xe5\xa3\xb0\xe6\x98\x8e</a> -\n  <a href="http://fankui.help.sogou.com/index.php/web/web/index?type=4&stype=hanyu" target="_blank">\xe6\x84\x8f\xe8\xa7\x81\xe5\x8f\x8d\xe9\xa6\x88\xe5\x8f\x8a\xe6\x8a\x95\xe8\xaf\x89</a>\n  -\n  <a target="_blank" href="http://corp.sogou.com/private.html" class="g">\xe9\x9a\x90\xe7\xa7\x81\xe6\x94\xbf\xe7\xad\x96</a> <br>\n  \xc2\xa9  SOGOU.COM - \xe4\xba\xacICP\xe8\xaf\x81050897\xe5\x8f\xb7 -\n  <a href="//www.beian.gov.cn/portal/registerSystemInfo?recordcode=11000002000025" target="_blank">\xe4\xba\xac\xe5\x85\xac\xe7\xbd\x91\xe5\xae\x89\xe5\xa4\x87\xe4\xba\xac\xe5\x85\xac\xe7\xbd\x91\xe5\xae\x89\xe5\xa4\x8711000002000025\xe5\x8f\xb7</a> <i class="ba"></i></div></div></div><script>window.__INITIAL_STATE__={"CONFIG":{"loadTime":1671804024312,"terminal":"","isIE":0,"wuid":1671804024312},"home":{"suggType":"","query":"","suggList":[],"tipsShow":false,"tipsString":"","queryLength":40},"detail":{"query":"\xe7\x89\x9b\xe6\x8e\xb0","resultType":"\xe8\xaf\xa6\xe6\x83\x85\xe9\xa1\xb5","queryType":"\xe8\xaf\x8d\xe8\xaf\xad","qType":"ciyu_pc","pageType":"detail_pc","resultData":{"ciyuData":{"text":"\xe7\x89\x9b\xe6\x8e\xb0","bspic":"","bsvideo":"","bsnum":0,"fanti":"\xe7\x89\x9b\xe6\x8e\xb0","pinyin":["ni\xc3\xba b\xc4\x81i"],"yuyin":["http:\\u002F\\u002Fpic04.sogoucdn.com\\u002Fs3\\u002Fa\\u002F201033\\u002F1e36a6a86b44bea6133c8501633bdb20.mp3"],"cygspic":"","cygstime":"","cygsvideo":"","cygsvideolink":""},"isduoyin":false,"zuci":[],"jyc":[],"fyc":[],"liju":[],"shiyi":["\xe5\xbe\x88\xe5\xbc\xba\xe6\x82\x8d\xe3\x80\x82"],"shiyiBase":[{"pinyin":"ni\xc3\xba b\xc4\x81i","shiyi":"\xe5\xbe\x88\xe5\xbc\xba\xe6\x82\x8d\xe3\x80\x82","shiyiHtml":"\\n             \\n            \\u003Cdd\\u003E\\n                                \\u003Cp\\u003E\\n                    \xe5\xbe\x88\xe5\xbc\xba\xe6\x82\x8d\xe3\x80\x82\\n                                    \\u003C\\u002Fp\\u003E\\n                            \\u003C\\u002Fdd\\u003E\\n\\n        "}],"shiyiDetail":[],"realWords":["\xe7\x89\x9b\xe6\xb0\x94","\xe7\x9c\x9f\xe7\x89\x9b","\xe7\x89\xb9\xe7\x89\x9b","\xe6\x82\x8d\xe7\x8a\xb7","\xe9\xb8\xb7\xe6\x89\x9e","\xe6\xad\xa6\xe6\x82\x8d","\xe6\x9e\xad\xe6\x9d\xb0","\xe6\x82\x8d\xe6\x88\x86"],"cygushi":"","jielong":[],"chuchu":"","pinyin":["ni\xc3\xba b\xc4\x81i"],"fromFilter":false,"query":"\xe7\x89\x9b\xe6\x8e\xb0","text":"","type1":"\xe8\xaf\x8d\xe8\xaf\xad","type2":"\xe8\xaf\xa6\xe6\x83\x85\xe9\xa1\xb5","pageType":"detail"},"ciyuData":{},"isduoyin":false,"shiyi":[],"hintList":[],"tabList":{"shiyi":{"text":"\xe5\x9f\xba\xe7\xa1\x80\xe9\x87\x8a\xe4\xb9\x89","tabId":"shiyi","dataKey":"shiyiBase","isShow":true},"shiyiDetail":{"text":"\xe8\xaf\xa6\xe7\xbb\x86\xe9\x87\x8a\xe4\xb9\x89","tabId":"shiyiDetail","isShow":false},"liju":{"text":"\xe4\xbe\x8b\xe5\x8f\xa5","tabId":"liju","isShow":false},"zuci":{"text":"\xe7\xbb\x84\xe8\xaf\x8d","tabId":"zuci","isShow":false},"jyc":{"text":"\xe8\xbf\x91\xe4\xb9\x89\xe8\xaf\x8d","tabId":"jyc","isShow":false},"fyc":{"text":"\xe5\x8f\x8d\xe4\xb9\x89\xe8\xaf\x8d","tabId":"fyc","isShow":false},"jielong":{"text":"\xe6\x88\x90\xe8\xaf\xad\xe6\x8e\xa5\xe9\xbe\x99","tabId":"jielong","isShow":false},"chuchu":{"text":"\xe5\x87\xba\xe5\xa4\x84","tabId":"chuchu","isShow":false},"cygushi":{"text":"\xe6\x88\x90\xe8\xaf\xad\xe6\x95\x85\xe4\xba\x8b","tabId":"cygushi","isShow":false},"relate":{"text":"\xe7\x9b\xb8\xe5\x85\xb3\xe5\xad\x97","tabId":"relate","isShow":false}},"tabShow":false,"bishunDialogeShow":false,"ssrQuery":"\xe5\x8b\x89\xe5\xbc\xba\xe7\x9a\x84\xe6\x84\x8f\xe6\x80\x9d\xe6\x98\xaf\xe4\xbb\x80\xe4\xb9\x88","noResult":false,"collectLen":0,"ssrUigsObj":{"uigs_id":"pageshow","page":"detail_pc","type":"ciyu_pc","tabshow":0,"voiceshow":1,"shiyishow":1,"shiyidetailshow":0,"lijushow":0,"jycshow":0,"fycshow":0,"relatedshow":1,"hintshow":0,"api_t":64,"render_t":65}},"route":{"name":"index","path":"\\u002F","hash":"","query":{"query":"\xe5\xa4\xa7\xe5\x99\xa8\xe6\x99\x9a\xe6\x88\x90"},"params":{},"fullPath":"\\u002F?query=%E5%A4%A7%E5%99%A8%E6%99%9A%E6%88%90","meta":{},"from":{"name":null,"path":"\\u002F","hash":"","query":{},"params":{},"fullPath":"\\u002F","meta":{}}}};(function(){var s;(s=document.currentScript||document.scripts[document.scripts.length-1]).parentNode.removeChild(s);}());</script><script src="//search.sogoucdn.com/hanyu/pc/static/js/vendors.8629acfd.js" defer></script><script src="//search.sogoucdn.com/hanyu/pc/static/js/index.128f2cfb.js" defer></script><script src="//search.sogoucdn.com/hanyu/pc/static/js/app.1a08a115.js" defer></script> </div><script crossorigin="anonymous" src="//dlweb.sogoucdn.com/vs/static/js/vsbundle.3ed018b1.js"></script></body></html> <!--zly-->'

通过浏览器查看 

 

5.随机获取20个代理

import json
import random


class userAgent(object):
    """
        直接将 网页的源码复制下载之后, 可以使用此类进行解析
        self.user_agent_data 是 读取 文件的,
    """

    def __init__(self, json_file="user_agent.json"):
        """
        :param json_file: 下载后内容保存的文件
        """
        self.json_file = json_file
        self.ua_data = self.user_agent_data().get("browsers")
        self.b = ['chrome', 'opera', 'firefox', 'safari', 'internetexplorer']
        # -------
        self.chrome = lambda: random.choice(self.ua_data.get("chrome"))
        self.opera = lambda: random.choice(self.ua_data.get("opera"))
        self.firefox = lambda: random.choice(self.ua_data.get("firefox"))
        self.safari = lambda: random.choice(self.ua_data.get("safari"))
        self.ie = lambda: random.choice(self.ua_data.get("internetexplorer"))
        self.random = lambda: random.choice(self.ua_data.get(random.choice(self.b)))

    def user_agent_data(self):
        with open(self.json_file, "r") as fp:
            data = fp.read()
        return json.loads(data)


ua = userAgent()

for i in range(20):
    print(ua.random())

 

Mozilla/5.0 (compatible; MSIE 10.6; Windows NT 6.1; Trident/5.0; InfoPath.2; SLCC1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727) 3gpp-gba UNTRUSTED/1.0
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:21.0) Gecko/20100101 Firefox/21.0
Mozilla/5.0 (X11; OpenBSD amd64; rv:28.0) Gecko/20100101 Firefox/28.0
Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko
Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.2309.372 Safari/537.36
Mozilla/5.0 (Android 2.2; Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.19.4 (KHTML, like Gecko) Version/5.0.3 Safari/533.19.4
Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; fr-ch) AppleWebKit/533.19.4 (KHTML, like Gecko) Version/5.0.3 Safari/533.19.4
Mozilla/1.22 (compatible; MSIE 10.0; Windows 3.1)
Mozilla/5.0 (Windows NT 6.1; rv:21.0) Gecko/20130401 Firefox/21.0
Opera/9.80 (X11; Linux x86_64; U; bg) Presto/2.8.131 Version/11.10
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36
Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36
Mozilla/5.0 (Windows NT 6.2; rv:22.0) Gecko/20130405 Firefox/22.0
Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; ja-jp) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27
Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1468.0 Safari/537.36
Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:24.0) Gecko/20100101 Firefox/24.0
Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; Media Center PC 4.0; SLCC1; .NET CLR 3.0.04320)
Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; ja-jp) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27
Opera/9.80 (X11; Linux i686; Ubuntu/14.10) Presto/2.12.388 Version/12.16
Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.90 Safari/537.36

总结

fake-useragent user-agent 字串史可以说明曾对 user-agent 说不的原因

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐