爬虫之UserAgent
UserAgent简介
UserAgent中文名为用户代理,是Http协议中的一部分,属于头域的组成部分,UserAgent也简称UA。它是一个特殊字符串头,是一种向访问网站提供你所使用的浏览器类型及版本、操作系统及版本、浏览器内核、等信息的标识。通过这个标识,用户所访问的网站可以显示不同的排版从而为用户提供更好的体验或者进行信息统计;例如用手机访问谷歌和电脑访问是不一样的,这些是谷歌根据访问者的UA来判断的。UA可以进行伪装。
浏览器的UA字串的标准格式:浏览器标识(操作系统标识;加密等级标识;浏览器语言)渲染引擎标识版本信息。但各个浏览器有所不同。
我们在做爬虫的时候,不是通过浏览器正常访问,所以会被很多网站禁止访问,这个时候我们就需要手动在headers里加上UA属性,来伪装成浏览器进行访问。
常见的UserAgent值
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | 1. PC端: safari5. 1 –MAC User - Agent:Mozilla / 5.0 (Macintosh;U;IntelMacOSX10_6_8;en - us)AppleWebKit / 534.50 (KHTML,likeGecko)Version / 5.1Safari / 534.50 safari5. 1 –Windows User - Agent:Mozilla / 5.0 (Windows;U;WindowsNT6. 1 ;en - us)AppleWebKit / 534.50 (KHTML,likeGecko)Version / 5.1Safari / 534.50 IE9. 0 User - Agent:Mozilla / 5.0 (compatible;MSIE9. 0 ;WindowsNT6. 1 ;Trident / 5.0 ; IE8. 0 User - Agent:Mozilla / 4.0 (compatible;MSIE8. 0 ;WindowsNT6. 0 ;Trident / 4.0 ) IE7. 0 User - Agent:Mozilla / 4.0 (compatible;MSIE7. 0 ;WindowsNT6. 0 ) IE6. 0 User - Agent:Mozilla / 4.0 (compatible;MSIE6. 0 ;WindowsNT5. 1 ) Firefox4. 0.1 –MAC User - Agent:Mozilla / 5.0 (Macintosh;IntelMacOSX10. 6 ;rv: 2.0 . 1 )Gecko / 20100101Firefox / 4.0 . 1 Firefox4. 0.1 –Windows User - Agent:Mozilla / 5.0 (WindowsNT6. 1 ;rv: 2.0 . 1 )Gecko / 20100101Firefox / 4.0 . 1 Opera11. 11 –MAC User - Agent:Opera / 9.80 (Macintosh;IntelMacOSX10. 6.8 ;U;en)Presto / 2.8 . 131Version / 11.11 Opera11. 11 –Windows User - Agent:Opera / 9.80 (WindowsNT6. 1 ;U;en)Presto / 2.8 . 131Version / 11.11 Chrome17. 0 –MAC User - Agent:Mozilla / 5.0 (Macintosh;IntelMacOSX10_7_0)AppleWebKit / 535.11 (KHTML,likeGecko)Chrome / 17.0 . 963.56Safari / 535.11 傲游(Maxthon) User - Agent:Mozilla / 4.0 (compatible;MSIE7. 0 ;WindowsNT5. 1 ;Maxthon2. 0 ) 腾讯TT User - Agent:Mozilla / 4.0 (compatible;MSIE7. 0 ;WindowsNT5. 1 ;TencentTraveler4. 0 ) 世界之窗(TheWorld) 2.x User - Agent:Mozilla / 4.0 (compatible;MSIE7. 0 ;WindowsNT5. 1 ) 世界之窗(TheWorld) 3.x User - Agent:Mozilla / 4.0 (compatible;MSIE7. 0 ;WindowsNT5. 1 ;TheWorld) 搜狗浏览器 1.x User - Agent:Mozilla / 4.0 (compatible;MSIE7. 0 ;WindowsNT5. 1 ;Trident / 4.0 ;SE2.XMetaSr1. 0 ;SE2.XMetaSr1. 0 ;.NETCLR2. 0.50727 ;SE2.XMetaSr1. 0 ) 360 浏览器 User - Agent:Mozilla / 4.0 (compatible;MSIE7. 0 ;WindowsNT5. 1 ; 360SE ) Avant User - Agent:Mozilla / 4.0 (compatible;MSIE7. 0 ;WindowsNT5. 1 ;AvantBrowser) GreenBrowser User - Agent:Mozilla / 4.0 (compatible;MSIE7. 0 ;WindowsNT5. 1 ) 2 、移动设备端: safariiOS4. 33 –iPhone User - Agent:Mozilla / 5.0 (iPhone;U;CPUiPhoneOS4_3_3likeMacOSX;en - us)AppleWebKit / 533.17 . 9 (KHTML,likeGecko)Version / 5.0 . 2Mobile / 8J2Safari / 6533.18 . 5 safariiOS4. 33 –iPodTouch User - Agent:Mozilla / 5.0 (iPod;U;CPUiPhoneOS4_3_3likeMacOSX;en - us)AppleWebKit / 533.17 . 9 (KHTML,likeGecko)Version / 5.0 . 2Mobile / 8J2Safari / 6533.18 . 5 safariiOS4. 33 –iPad User - Agent:Mozilla / 5.0 (iPad;U;CPUOS4_3_3likeMacOSX;en - us)AppleWebKit / 533.17 . 9 (KHTML,likeGecko)Version / 5.0 . 2Mobile / 8J2Safari / 6533.18 . 5 AndroidN1 User - Agent:Mozilla / 5.0 (Linux;U;Android2. 3.7 ;en - us;NexusOneBuild / FRF91)AppleWebKit / 533.1 (KHTML,likeGecko)Version / 4.0MobileSafari / 533.1 AndroidQQ浏览器Forandroid User - Agent:MQQBrowser / 26Mozilla / 5.0 (Linux;U;Android2. 3.7 ;zh - cn;MB200Build / GRJ22;CyanogenMod - 7 )AppleWebKit / 533.1 (KHTML,likeGecko)Version / 4.0MobileSafari / 533.1 AndroidOperaMobile User - Agent:Opera / 9.80 (Android2. 3.4 ;Linux;OperaMobi / build - 1107180945 ;U;en - GB)Presto / 2.8 . 149Version / 11.10 AndroidPadMotoXoom User - Agent:Mozilla / 5.0 (Linux;U;Android3. 0 ;en - us;XoomBuild / HRI39)AppleWebKit / 534.13 (KHTML,likeGecko)Version / 4.0Safari / 534.13 BlackBerry User - Agent:Mozilla / 5.0 (BlackBerry;U;BlackBerry9800;en)AppleWebKit / 534.1 + (KHTML,likeGecko)Version / 6.0 . 0.337MobileSafari / 534.1 + WebOSHPTouchpad User - Agent:Mozilla / 5.0 (hp - tablet;Linux;hpwOS / 3.0 . 0 ;U;en - US)AppleWebKit / 534.6 (KHTML,likeGecko)wOSBrowser / 233.70Safari / 534.6TouchPad / 1.0 NokiaN97 User - Agent:Mozilla / 5.0 (SymbianOS / 9.4 ;Series60 / 5.0NokiaN97 - 1 / 20.0 . 019 ;Profile / MIDP - 2.1Configuration / CLDC - 1.1 )AppleWebKit / 525 (KHTML,likeGecko)BrowserNG / 7.1 . 18124 WindowsPhoneMango User - Agent:Mozilla / 5.0 (compatible;MSIE9. 0 ;WindowsPhoneOS7. 5 ;Trident / 5.0 ;IEMobile / 9.0 ;HTC;Titan) UC无 User - Agent:UCWEB7. 0.2 . 37 / 28 / 999 UC标准 User - Agent:NOKIA5700 / UCWEB7. 0.2 . 37 / 28 / 999 UCOpenwave User - Agent:Openwave / UCWEB7. 0.2 . 37 / 28 / 999 UCOpera User - Agent:Mozilla / 4.0 (compatible;MSIE6. 0 ;)Opera / UCWEB7. 0.2 . 37 / 28 / 999 |
我们用的时候直接复制即可,放到headers里的对应User-Agent参数
UserAgent的两种添加方法
1 直接定义一个headers字典,然后传递给Request类来实例化一个对象,然后在传给urlopen,格式如下:
1 2 3 4 5 6 7 8 9 10 | from urllib import request url = 'http://baidu.com' headers = { 'User-Agent' : 'Mozilla/5.0(iPhone;U;CPUiPhoneOS4_3_3likeMacOSX;en-us)AppleWebKit/533.17.9(KHTML,likeGecko)Version/5.0.2Mobile/8J2Safari/6533.18.5' } req = request.Request(url,headers = headers) response = request.urlopen(req) print (response.read().decode()) |
2.使用add_header()方法
1 2 3 4 5 6 7 8 9 | from urllib import request url = 'http://baidu.com' req = request.Request(url) req.add_header( 'User-Agent' , 'Mozilla/5.0(iPhone;U;CPUiPhoneOS4_3_3likeMacOSX;en-us)AppleWebKit/533.17.9(KHTML,likeGecko)Version/5.0.2Mobile/8J2Safari/6533.18.5' ) response = request.urlopen(req) print (response.read().decode()) |
注意:add_header()方法,接收的是两个参数,分别为键值对的兼和值,而非一个字典
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步