如果网站对请求头部有限制,短时间内频繁访问会被锁定,可以使用随机请求头部伪装不同浏览器
使用 python 第三方模块 fake_useragent 随机生成请求头部 UserAgent
pip安装依赖包
pip install fake_useragent --index-url https://pypi.douban.com/simple
查看版本
D:\soft>pip show fake_useragent
Name: fake-useragent
Version: 0.1.11
Summary: Up to date simple useragent faker with real world database
Home-page: https://github.com/hellysmile/fake-useragent
Author: [email protected]
Author-email: [email protected]
License: UNKNOWN
Location: e:\python36\lib\site-packages
Requires:
Required-by: requests-html
可以指定浏览器名称,模拟对应浏览器请求头部UserAgent
from fake_useragent import UserAgent
# 指定浏览器名称
ua = UserAgent()
print(ua.ie)
print(ua.firefox)
print(ua.chrome)
每次运行生成的浏览器版本都不一样
Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; chromeframe/13.0.782.215)
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:25.0) Gecko/20100101 Firefox/25.0
Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36
调用random方法随机生成请求头部
from fake_useragent import UserAgent
ua = UserAgent()
headers = {"User-Agent": ua.random}
print(headers)
每次运行结果都会不一样
{'User-Agent': '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'}
查看不同User-Agent地址:http://fake-useragent.herokuapp.com/browsers/0.1.5