lzc527054845 发表于 2019-1-24 22:27

【笔记】自己做的UserAgent

#!/user/bin/env python
# -*- coding: utf-8 -*-
# @Time   : 2018/12/8 22:15
import random

headers = """
Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/5.0)
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1944.0 Safari/537.36
Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2225.0 Safari/537.36
Mozilla/5.0 (Windows NT 4.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1664.3 Safari/537.36
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1944.0 Safari/537.36
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36
Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.67 Safari/537.36
Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2225.0 Safari/537.36
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1664.3 Safari/537.36
Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36
Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.67 Safari/537.36
Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.90 Safari/537.36
Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36 Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36
Mozilla/5.0 (X11; CrOS i686 3912.101.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36
Mozilla/5.0 (Windows NT 6.1; rv:27.3) Gecko/20130101 Firefox/27.3
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.67 Safari/537.36
Mozilla/5.0 (X11; CrOS i686 4319.74.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36
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 6.2; Win64; x64;) Gecko/20100101 Firefox/20.0
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/4E423F
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.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0)Gecko/20100101 Firefox/18.0
Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.14 (KHTML, like Gecko) Chrome/24.0.1292.0 Safari/537.14
Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.93 Safari/537.36
Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.90 Safari/537.36
Mozilla/5.0 (Windows NT 5.1; rv:21.0) Gecko/20130331 Firefox/21.0
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 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.16 Safari/537.36
Mozilla/5.0 (X11; CrOS i686 3912.101.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1623.0 Safari/537.36
Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1467.0 Safari/537.36
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36
Mozilla/5.0 (Windows NT 6.1; rv:21.0) Gecko/20130401 Firefox/21.0
Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36
Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.67 Safari/537.36
Mozilla/5.0 (Windows NT 6.4; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2225.0 Safari/537.36
Mozilla/5.0 (Windows; U; Windows NT 6.0; hu-HU) AppleWebKit/533.19.4 (KHTML, like Gecko) Version/5.0.3 Safari/533.19.4
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36
Mozilla/5.0 (X11; OpenBSD amd64; rv:28.0) Gecko/20100101 Firefox/28.0
Mozilla/5.0 (Microsoft Windows NT 6.2.9200.0); rv:22.0) Gecko/20130405 Firefox/22.0
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.67 Safari/537.36
Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2225.0 Safari/537.36
Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1866.237 Safari/537.36
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:25.0) Gecko/20100101 Firefox/25.0
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1944.0 Safari/537.36
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1944.0 Safari/537.36
Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.2117.157 Safari/537.36
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20130401 Firefox/21.0
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1664.3 Safari/537.36
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1944.0 Safari/537.36
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1623.0 Safari/537.36
Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.62 Safari/537.36
Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.3319.102 Safari/537.36
Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)
Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1467.0 Safari/537.36
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1309.0 Safari/537.17
Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.3319.102 Safari/537.36
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.517 Safari/537.36
Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36
Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; de-at) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36
Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20130405 Firefox/22.0
Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2224.3 Safari/537.36
Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.2 Safari/537.36
Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.93 Safari/537.36
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36
Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36
Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.14 (KHTML, like Gecko) Chrome/24.0.1292.0 Safari/537.14
Mozilla/5.0 (Windows NT 6.1; rv:21.0) Gecko/20100101 Firefox/21.0
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/4E423F
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.517 Safari/537.36
Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1464.0 Safari/537.36
Opera/9.80 (X11; Linux i686; Ubuntu/14.10) Presto/2.12.388 Version/12.16
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1944.0 Safari/537.36
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1944.0 Safari/537.36
Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2225.0 Safari/537.36
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36
Mozilla/5.0 (Windows NT 5.1; rv:21.0) Gecko/20100101 Firefox/21.0
Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.15 (KHTML, like Gecko) Chrome/24.0.1295.0 Safari/537.15
Mozilla/5.0 (Windows NT 4.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.517 Safari/537.36
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 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.60 Safari/537.17
Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.90 Safari/537.36
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1664.3 Safari/537.36
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.60 Safari/537.17
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 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36
Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.67 Safari/537.36
Mozilla/5.0 (X11; CrOS i686 3912.101.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36
Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-HK) AppleWebKit/533.18.1 (KHTML, like Gecko) Version/5.0.2 Safari/533.18.5
Mozilla/5.0 (Windows NT 6.2; Win64; x64; rv:27.0) Gecko/20121011 Firefox/27.0
Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.2117.157 Safari/537.36
Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.2 Safari/537.36
"""


def head():
    header =headers.split('\n')
    length = len(header)
    return {"User-Agent": header0, length-1)]}
# 利用fake_useragent中的UserAgent().random 获取随机headers



liangyun 发表于 2019-3-15 16:15

本帖最后由 liangyun 于 2019-3-15 16:22 编辑

可以使用faker库,具体方案:
```python
from faker import Faker

f = Faker('zh_CN')
for i in range(100):
    print(f.user_agent())
```
结果:
```
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/5310 (KHTML, like Gecko) Chrome/23.0.818.0 Safari/5310
Mozilla/5.0 (Windows NT 6.1) AppleWebKit/5341 (KHTML, like Gecko) Chrome/32.0.893.0 Safari/5341
Mozilla/5.0 (compatible; MSIE 6.0; Windows NT 5.1; Trident/5.1)
Opera/9.22.(X11; Linux i686; lg-UG) Presto/2.9.187 Version/11.00
Mozilla/5.0 (iPod; U; CPU iPhone OS 4_2 like Mac OS X; lo-LA) AppleWebKit/531.11.5 (KHTML, like Gecko) Version/4.0.5 Mobile/8B117 Safari/6531.11.5
Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_2; rv:1.9.5.20) Gecko/2018-12-20 16:18:43 Firefox/5.0
Mozilla/5.0 (Windows; U; Windows NT 6.2) AppleWebKit/532.45.4 (KHTML, like Gecko) Version/5.0 Safari/532.45.4
Opera/8.67.(Windows NT 4.0; bn-IN) Presto/2.9.176 Version/12.00
Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/3.1)
Opera/9.41.(Windows 98; kk-KZ) Presto/2.9.176 Version/10.00
Opera/8.57.(X11; Linux i686; zu-ZA) Presto/2.9.169 Version/11.00
Mozilla/5.0 (Macintosh; PPC Mac OS X 10_11_8; rv:1.9.3.20) Gecko/2015-11-13 02:30:00 Firefox/6.0
Mozilla/5.0 (compatible; MSIE 5.0; Windows NT 5.1; Trident/3.0)
Opera/9.63.(Windows CE; quz-PE) Presto/2.9.165 Version/12.00
Mozilla/5.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/3.1)
Mozilla/5.0 (compatible; MSIE 6.0; Windows 98; Win 9x 4.90; Trident/3.0)
Opera/8.66.(X11; Linux i686; nso-ZA) Presto/2.9.165 Version/12.00
Mozilla/5.0 (Windows NT 5.01; gd-GB; rv:1.9.1.20) Gecko/2016-06-14 12:56:45 Firefox/3.8
Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3 like Mac OS X; gd-GB) AppleWebKit/535.10.2 (KHTML, like Gecko) Version/3.0.5 Mobile/8B116 Safari/6535.10.2
Mozilla/5.0 (X11; Linux i686) AppleWebKit/5350 (KHTML, like Gecko) Chrome/27.0.855.0 Safari/5350
Opera/9.80.(Windows 98; sid-ET) Presto/2.9.170 Version/10.00
Mozilla/5.0 (compatible; MSIE 6.0; Windows NT 4.0; Trident/4.1)
Mozilla/5.0 (Windows CE) AppleWebKit/5311 (KHTML, like Gecko) Chrome/51.0.892.0 Safari/5311
Mozilla/5.0 (Windows; U; Windows 95) AppleWebKit/534.23.3 (KHTML, like Gecko) Version/5.1 Safari/534.23.3
Mozilla/5.0 (Windows; U; Windows NT 5.1) AppleWebKit/535.17.4 (KHTML, like Gecko) Version/4.0.2 Safari/535.17.4
Opera/8.80.(X11; Linux i686; cmn-TW) Presto/2.9.174 Version/11.00
Mozilla/5.0 (Windows; U; Windows NT 6.1) AppleWebKit/531.24.1 (KHTML, like Gecko) Version/4.0.5 Safari/531.24.1
Mozilla/5.0 (Windows; U; Windows NT 5.2) AppleWebKit/532.45.7 (KHTML, like Gecko) Version/5.0.5 Safari/532.45.7
Opera/8.64.(Windows NT 4.0; unm-US) Presto/2.9.180 Version/10.00
Mozilla/5.0 (Windows NT 5.1; ug-CN; rv:1.9.0.20) Gecko/2018-04-24 03:43:26 Firefox/14.0
Opera/9.28.(X11; Linux i686; ss-ZA) Presto/2.9.181 Version/10.00
Mozilla/5.0 (X11; Linux i686; rv:1.9.6.20) Gecko/2010-02-20 12:41:24 Firefox/3.8
Opera/9.42.(X11; Linux i686; apn-IN) Presto/2.9.171 Version/10.00
Mozilla/5.0 (compatible; MSIE 9.0; Windows CE; Trident/4.0)
Mozilla/5.0 (compatible; MSIE 7.0; Windows NT 5.01; Trident/3.0)
Opera/8.99.(X11; Linux i686; pa-IN) Presto/2.9.178 Version/12.00
Mozilla/5.0 (Windows 98) AppleWebKit/5330 (KHTML, like Gecko) Chrome/14.0.845.0 Safari/5330
Mozilla/5.0 (Windows NT 5.2) AppleWebKit/5311 (KHTML, like Gecko) Chrome/20.0.871.0 Safari/5311
Mozilla/5.0 (compatible; MSIE 5.0; Windows NT 4.0; Trident/5.1)
Mozilla/5.0 (Windows; U; Windows NT 5.0) AppleWebKit/532.6.4 (KHTML, like Gecko) Version/4.1 Safari/532.6.4
Mozilla/5.0 (X11; Linux i686; rv:1.9.6.20) Gecko/2011-12-16 06:28:04 Firefox/3.6.19
Mozilla/5.0 (compatible; MSIE 5.0; Windows NT 6.1; Trident/5.0)
Opera/9.75.(Windows CE; csb-PL) Presto/2.9.167 Version/10.00
Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_11_0 rv:3.0; ka-GE) AppleWebKit/535.42.3 (KHTML, like Gecko) Version/4.0 Safari/535.42.3
Mozilla/5.0 (Windows 98; Win 9x 4.90) AppleWebKit/5360 (KHTML, like Gecko) Chrome/40.0.872.0 Safari/5360
Mozilla/5.0 (Windows NT 4.0; am-ET; rv:1.9.1.20) Gecko/2013-02-10 21:33:35 Firefox/3.8
Mozilla/5.0 (Windows NT 4.0; yo-NG; rv:1.9.0.20) Gecko/2011-04-08 21:42:13 Firefox/3.8
Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 5.0; Trident/4.1)
Opera/9.81.(Windows NT 6.1; an-ES) Presto/2.9.183 Version/12.00
Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_9_9; rv:1.9.5.20) Gecko/2013-10-24 19:19:43 Firefox/3.6.19
Mozilla/5.0 (Windows; U; Windows NT 5.1) AppleWebKit/535.26.6 (KHTML, like Gecko) Version/5.0.1 Safari/535.26.6
Opera/9.99.(Windows 98; Win 9x 4.90; sa-IN) Presto/2.9.178 Version/12.00
Opera/9.82.(X11; Linux x86_64; cv-RU) Presto/2.9.179 Version/11.00
Mozilla/5.0 (X11; Linux i686) AppleWebKit/5331 (KHTML, like Gecko) Chrome/58.0.899.0 Safari/5331
Mozilla/5.0 (Macintosh; PPC Mac OS X 10_8_2) AppleWebKit/5330 (KHTML, like Gecko) Chrome/48.0.898.0 Safari/5330
Mozilla/5.0 (Windows NT 4.0) AppleWebKit/5360 (KHTML, like Gecko) Chrome/60.0.806.0 Safari/5360
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_9; rv:1.9.4.20) Gecko/2018-12-08 09:58:17 Firefox/5.0
Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10_8_1 rv:3.0; ve-ZA) AppleWebKit/534.28.6 (KHTML, like Gecko) Version/4.1 Safari/534.28.6
Mozilla/5.0 (compatible; MSIE 5.0; Windows NT 5.01; Trident/3.0)
Mozilla/5.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0)
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/5320 (KHTML, like Gecko) Chrome/24.0.811.0 Safari/5320
Mozilla/5.0 (X11; Linux i686) AppleWebKit/5310 (KHTML, like Gecko) Chrome/20.0.865.0 Safari/5310
Opera/8.10.(Windows CE; quz-PE) Presto/2.9.183 Version/10.00
Opera/8.73.(Windows NT 5.1; st-ZA) Presto/2.9.177 Version/11.00
Mozilla/5.0 (compatible; MSIE 7.0; Windows NT 4.0; Trident/5.1)
Mozilla/5.0 (X11; Linux i686) AppleWebKit/5330 (KHTML, like Gecko) Chrome/43.0.860.0 Safari/5330
Mozilla/5.0 (Windows NT 6.0) AppleWebKit/5342 (KHTML, like Gecko) Chrome/23.0.812.0 Safari/5342
Opera/8.14.(Windows NT 5.1; hr-HR) Presto/2.9.175 Version/11.00
Mozilla/5.0 (Windows 98; Win 9x 4.90) AppleWebKit/5332 (KHTML, like Gecko) Chrome/58.0.898.0 Safari/5332
Mozilla/5.0 (X11; Linux i686; rv:1.9.7.20) Gecko/2010-09-28 17:07:15 Firefox/3.8
Mozilla/5.0 (Windows NT 6.2; ln-CD; rv:1.9.0.20) Gecko/2018-11-21 11:16:49 Firefox/3.6.19
Mozilla/5.0 (iPod; U; CPU iPhone OS 3_3 like Mac OS X; dz-BT) AppleWebKit/535.32.3 (KHTML, like Gecko) Version/3.0.5 Mobile/8B118 Safari/6535.32.3
Mozilla/5.0 (X11; Linux i686) AppleWebKit/5351 (KHTML, like Gecko) Chrome/51.0.879.0 Safari/5351
Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/4.0)
Mozilla/5.0 (X11; Linux i686) AppleWebKit/5351 (KHTML, like Gecko) Chrome/54.0.874.0 Safari/5351
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_4; rv:1.9.3.20) Gecko/2011-02-15 02:07:05 Firefox/3.8
Mozilla/5.0 (compatible; MSIE 7.0; Windows 98; Trident/5.0)
Opera/9.97.(Windows CE; he-IL) Presto/2.9.178 Version/10.00
Mozilla/5.0 (X11; Linux i686) AppleWebKit/5360 (KHTML, like Gecko) Chrome/34.0.889.0 Safari/5360
Opera/9.78.(X11; Linux x86_64; bhb-IN) Presto/2.9.160 Version/12.00
Mozilla/5.0 (Windows NT 6.0) AppleWebKit/5311 (KHTML, like Gecko) Chrome/46.0.812.0 Safari/5311
Opera/9.24.(X11; Linux x86_64; nhn-MX) Presto/2.9.175 Version/12.00
Mozilla/5.0 (Macintosh; PPC Mac OS X 10_10_1 rv:2.0; ca-IT) AppleWebKit/533.47.2 (KHTML, like Gecko) Version/5.1 Safari/533.47.2
Mozilla/5.0 (X11; Linux i686; rv:1.9.5.20) Gecko/2018-03-29 06:02:54 Firefox/14.0
Mozilla/5.0 (Macintosh; PPC Mac OS X 10_7_8 rv:5.0; ta-IN) AppleWebKit/534.36.5 (KHTML, like Gecko) Version/5.0.1 Safari/534.36.5
Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_7_1 rv:6.0; ast-ES) AppleWebKit/534.2.7 (KHTML, like Gecko) Version/5.0 Safari/534.2.7
Mozilla/5.0 (compatible; MSIE 7.0; Windows NT 6.2; Trident/5.1)
Opera/8.75.(Windows NT 5.0; quz-PE) Presto/2.9.168 Version/10.00
Mozilla/5.0 (compatible; MSIE 7.0; Windows NT 4.0; Trident/3.0)
Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10_10_7 rv:2.0; hy-AM) AppleWebKit/532.48.3 (KHTML, like Gecko) Version/5.0.4 Safari/532.48.3
Mozilla/5.0 (Windows NT 6.2) AppleWebKit/5352 (KHTML, like Gecko) Chrome/33.0.810.0 Safari/5352
Opera/8.48.(X11; Linux x86_64; quz-PE) Presto/2.9.189 Version/11.00
Opera/8.80.(X11; Linux x86_64; id-ID) Presto/2.9.186 Version/11.00
Mozilla/5.0 (compatible; MSIE 6.0; Windows 95; Trident/3.1)
Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_9_6; rv:1.9.6.20) Gecko/2014-06-20 07:53:15 Firefox/3.8
Opera/9.25.(X11; Linux x86_64; mn-MN) Presto/2.9.184 Version/12.00
Mozilla/5.0 (X11; Linux i686; rv:1.9.7.20) Gecko/2015-12-14 08:03:25 Firefox/6.0
Mozilla/5.0 (compatible; MSIE 6.0; Windows NT 5.01; Trident/3.1)
Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3 like Mac OS X; nds-DE) AppleWebKit/532.20.1 (KHTML, like Gecko) Version/4.0.5 Mobile/8B119 Safari/6532.20.1
Mozilla/5.0 (compatible; MSIE 5.0; Windows 95; Trident/3.0)
```


如果想生成指定的UA,
还可以使用fake_useragent库
```python
from fake_useragent import UserAgent

ua = UserAgent()
print(ua.random)
print(ua.chrome)
print(ua.firefox)
print(ua.safari)
print(ua.ie)
print(ua.opera)
```
效果:
```
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/534.55.3 (KHTML, like Gecko) Version/5.1.3 Safari/534.53.10
Mozilla/5.0 (compatible; MSIE 7.0; Windows NT 5.0; Trident/4.0; FBSMTWB; .NET CLR 2.0.34861; .NET CLR 3.0.3746.3218; .NET CLR 3.5.33652; msn OptimizedIE8;ENUS)
Opera/9.80 (Windows NT 6.1; U; zh-cn) Presto/2.7.62 Version/11.01
Mozilla/5.0 (Windows NT 4.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36
Mozilla/5.0 (X11; OpenBSD amd64; rv:28.0) Gecko/20100101 Firefox/28.0
Mozilla/5.0 (Windows; U; Windows NT 6.0; tr-TR) AppleWebKit/533.18.1 (KHTML, like Gecko) Version/5.0.2 Safari/533.18.5
```

打字的小强 发表于 2019-1-25 10:24

这个随机选取函数写的很不错,赞

88268459 发表于 2019-1-25 16:52

这个也可以有哦,吼吼,谢楼主分享!

lmmmr9 发表于 2019-1-26 14:12

好东西。感谢分享

小谷 发表于 2019-1-26 19:06

好东西感谢分享

hero52pojie 发表于 2019-1-26 23:20

return {"User-Agent": random.choice(header)}
就可以了

wode_0yaya 发表于 2019-1-27 00:07

这个好玩,我拿回家试了下,觉得挺有意思。

sdtfll 发表于 2019-3-15 19:22

收藏,,,,,,

py看考场 发表于 2019-3-16 11:59

这个构造过程巧妙啊
页: [1] 2 3
查看完整版本: 【笔记】自己做的UserAgent