UserAgent使用
GitHub: https://github.com/hellysmile/fake-useragent
安装: pip3 install fake-useragent
查看useragent: http://fake-useragent.herokuapp.com/browsers/0.1.5
from fake_useragent import UserAgent
ua = UserAgent()
# 随机打印ie浏览器任意版本
res = ua.ie
print(res, type(res)) # Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/5.0; chromeframe/11.0.696.57) <class 'str'>
# 随机打印firefox浏览器任意版本
res = ua.firefox
print(res, type(res)) # Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1 <class 'str'>
# 随机打印chrome浏览器任意版本
res = ua.chrome
print(res, type(res)) # Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1866.237 Safari/537.36 <class 'str'>
# 随机打印任意厂家的浏览器
res = ua.random
print(res, type(res)) # Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.67 Safari/537.36 <class 'str'>
res = UserAgent().random
print(res, type(res)) # Mozilla/5.0 (Windows NT 6.1; rv:27.3) Gecko/20130101 Firefox/27.3 <class 'str'>