一、安装fake-useragent库:
pip install fake-useragent
二、使用方法:
1、导入fake-useragent库
from fake_useragent import UserAgent
2、实例化一个对象
ua = UserAgent()
3、随机生成IE浏览器的UserAgent
ua.ie
u'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/4.0; GTB7.4; InfoPa
th.3; SV1; .NET CLR 3.1.76908; WOW64; en-US)'
ua.ie
u'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .N
ET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Media Center PC 6.0)'
ua.ie
u'Mozilla/5.0 (compatible; MSIE 10.6; Windows NT 6.1; Trident/5.0; InfoPath.2; S
LCC1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727) 3gpp-gba U
NTRUSTED/1.0'
ua.ie
u'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/4.0; InfoPath.2; S
V1; .NET CLR 2.0.50727; WOW64)'
ua.ie
u'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0'
4、随机生成firefox浏览器的UserAgent
ua.firefox
u'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0'
ua.firefox
u'Mozilla/5.0 (Windows NT 5.0; rv:21.0) Gecko/20100101 Firefox/21.0'
ua.firefox
u'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0'
ua.firefox
u'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:21.0) Gecko/20100101 Firefox/21.0'
ua.firefox
u'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0'
5、随机生成chrome浏览器的UserAgent
ua.chrome
u'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'
ua.chrome
u'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'
ua.chrome
u'Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40
.0.2214.93 Safari/537.36'
ua.chrome
u'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chr
ome/41.0.2225.0 Safari/537.36'
ua.chrome
u'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'
6、随机生成safari浏览器的UserAgent
ua.safari
u'Mozilla/5.0 (Windows; U; Windows NT 6.1; de-DE) AppleWebKit/533.20.25 (KHTML,
like Gecko) Version/5.0.3 Safari/533.19.4'
ua.safari
u'Mozilla/5.0 (Windows; U; Windows NT 6.1; ja-JP) AppleWebKit/533.20.25 (KHTML,
like Gecko) Version/5.0.3 Safari/533.19.4'
ua.safari
u'Mozilla/5.0 (Windows; U; Windows NT 6.1; sv-SE) AppleWebKit/533.19.4 (KHTML, l
ike Gecko) Version/5.0.3 Safari/533.19.4'
ua.safari
u'Mozilla/5.0 (Windows; U; Windows NT 5.1; ru-RU) AppleWebKit/533.18.1 (KHTML, l
ike Gecko) Version/5.0.2 Safari/533.18.5'
7、随机生成google浏览器的UserAgent
ua.google
u'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.
0.2224.3 Safari/537.36'
ua.google
u'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.
0.1453.93 Safari/537.36'
ua.google
u'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chr
ome/41.0.2226.0 Safari/537.36'
ua.google
u'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.
0.3319.102 Safari/537.36'
8、随机生成opera浏览器的UserAgent
ua.opera
u'Mozilla/5.0 (Windows NT 6.1; U; de; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6 O
pera 11.01'
ua.opera
u'Opera/9.80 (Windows NT 6.1; Opera Tablet/15165; U; en) Presto/2.8.149 Version/
11.1'
ua.opera
u'Opera/9.80 (Windows NT 6.1; U; sv) Presto/2.7.62 Version/11.01'
ua.opera
u'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; de) Opera 11.51'
ua.opera
u'Opera/9.80 (Windows NT 6.1; U; pl) Presto/2.7.62 Version/11.00'
9、随机生成各种浏览器的UserAgent
ua.random
u'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chr
ome/27.0.1453.93 Safari/537.36'
ua.random
u'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chr
ome/30.0.1599.17 Safari/537.36'
ua.random
u'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chr
ome/30.0.1599.17 Safari/537.36'
ua.random
u'Mozilla/5.0 (Windows NT 6.4; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chr
ome/41.0.2225.0 Safari/537.36'
ua.random
u'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'
ua.random
u'Mozilla/5.0 (X11; CrOS i686 4319.74.0) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/29.0.1547.57 Safari/537.36'
ua.random
u'Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko
) Chrome/32.0.1667.0 Safari/537.36'
ua.random
u'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:24.0) Gecko/20100101 Firefox/24.0'
ua.random
u'Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40
.0.2214.93 Safari/537.36'
ua.random
u'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2;
.NET CLR 2.0.50727; InfoPath.3; .NET4.0C; .NET4.0E; .NET CLR 3.5.30729; .NET CLR
3.0.30729; MS-RTC LM 8)'
三、查看fake-useragent版本
1、使用pip list命令查看fake-useragent版本
(scrapytest) F:\python-virtualenv\scrapytest\Scripts>pip list
attrs (17.3.0)
Automat (0.6.0)
certifi (2017.11.5)
cffi (1.11.2)
chardet (3.0.4)
constantly (15.1.0)
cryptography (2.1.3)
cssselect (1.0.1)
enum34 (1.1.6)
fake-useragent (0.1.8)
hyperlink (17.3.1)
idna (2.6)
访问https://fake-useragent.herokuapp.com/browsers/0.1.8查看useragent库
四、user-agent库
https://fake-useragent.herokuapp.com/browsers/0.1.4
json格式显示的,可以把内容复制到以下网址进行json视图显示
https://www.bejson.com/jsonviewernew/