fake_useragent
fake_useragent第三方库,来实现随机请求头的设置
安装
pip3 install fake-useragent
用法
from fake_useragent import UserAgent
ua = UserAgent()
print(ua.ie)
print(ua.opera)
print(ua.chrome)
print(ua.firefox)
print(ua.safari)
print(ua.random) 获取任意请求头
报错:
fake_useragent.errors.FakeUserAgentError: Maximum amount of retries reached
解决
- 禁用服务器缓存
ua = UserAgent(use_cache_server=False)
- 忽略ssl验证
ua = UserAgent(verify_ssl=False)
- 使用json文件
1、wget https://fake-useragent.herokuapp.com/browsers/0.1.11
2、mv 0.1.11 fake_useragent_0.1.11.json
ua = UserAgent(path="./fake_useragent_0.1.11.json") #path后面跟下载的json文件路径
例
#! /usr/bin/python3
# -*- congfig:utf-8 -*-
from fake_useragent import UserAgent
def test():
ua = UserAgent()
ua = UserAgent(path="./fake_useragent_0.1.11.json")
return ua.random
if __name__ == "__main__":
for i in range(5):
print(test())
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; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.60 Safari/537.17
Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1467.0 Safari/537.36
Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0
Mozilla/5.0 (Windows; U; Windows NT 6.0; ja-JP) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27