爬虫自动获取UserAgent
程序员文章站
2022-05-09 11:17:37
...
导入
from fake_useragent import UserAgent, FakeUserAgentError
import random
获取用户代理对象
try:
ua = UserAgent()
except FakeUserAgentError:
pass
用随机随机获取一个用户代理,并加入到页眉
base_headers = {
'User-Agent': ua.random ,
'Accept-Encoding': 'gzip,deflate,sdch' ,
'Accept-Language': 'zh-CN,zh; q = 0.8',
}
最后带着夏理请求
response = requests.get(url, headers = base_headers)
推荐阅读