一、FakeUserAgentError(‘Maximum amount of retries reached!’)
禁用服务器缓存
ua = UserAgent(use_cache_server=False)
无效,不缓存数据:
ua = UserAgent(cache=False)
无效,忽略ssl验证:
ua = UserAgent(verify_ssl=False)
解决办法:(以上都无效,则使用本地保存获取)
下载:
https://fake-useragent.herokuapp.com/browsers/0.1.11
上面的网址打不开的话,直接用下面的网址
http://fake-useragent.herokuapp.com/browsers/0.1.11
并另存为:
fake_useragent.json
获取user_agent函数
def get_useragent():
location = os.getcwd() + '/fake_useragent.json'
ua = fake_useragent.UserAgent(path=location)
return ua.random
还有一种方法就是直接把该模块的源文件修改,也许是应为公司限制的原因
https://fake-useragent.herokuapp.com/browsers/0.1.11 网址打不开,我直接换成
http://fake-useragent.herokuapp.com/browsers/0.1.11 ,这样完全可以使用,而且不必自己写函数,也不用下载文件
二、日志文件中文出现乱码
网上好多说如下设置就可以:
logging.FileHandler(filename=‘example.log’, encoding=‘utf-8’)
可是我的并没啥用
后来发现FileHandler配置文件
直接把参数encoding=None 设置成如上图,完美解决