话说这个 fake_useragent 是实在烦人, 每隔一段时间执行脚本都是这个东西报错,
来, 给大家康康今天又作了什么妖,报了什么错
Connected to pydev debugger (build 181.5087.20)
Traceback (most recent call last):
File "D:\py\lib\urllib\request.py", line 1317, in do_open
encode_chunked=req.has_header('Transfer-encoding'))
File "D:\py\lib\http\client.py", line 1229, in request
self._send_request(method, url, body, headers, encode_chunked)
File "D:\py\lib\http\client.py", line 1275, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "D:\py\lib\http\client.py", line 1224, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "D:\py\lib\http\client.py", line 1016, in _send_output
self.send(msg)
File "D:\py\lib\http\client.py", line 956, in send
self.connect()
File "D:\py\lib\http\client.py", line 1384, in connect
super().connect()
File "D:\py\lib\http\client.py", line 928, in connect
(self.host,self.port), self.timeout, self.source_address)
File "D:\py\lib\socket.py", line 727, in create_connection
raise err
File "D:\py\lib\socket.py", line 716, in create_connection
sock.connect(sa)
socket.timeout: timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\py\lib\site-packages\fake_useragent\utils.py", line 67, in get
context=context,
File "D:\py\lib\urllib\request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "D:\py\lib\urllib\request.py", line 525, in open
response = self._open(req, data)
File "D:\py\lib\urllib\request.py", line 543, in _open
'_open', req)
File "D:\py\lib\urllib\request.py", line 503, in _call_chain
result = func(*args)
File "D:\py\lib\urllib\request.py", line 1360, in https_open
context=self._context, check_hostname=self._check_hostname)
File "D:\py\lib\urllib\request.py", line 1319, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error timed out>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\DELL\.IntelliJIdea2018.1\config\plugins\python\helpers\pydev\pydevd.py", line 1664, in <module>
main()
File "C:\Users\DELL\.IntelliJIdea2018.1\config\plugins\python\helpers\pydev\pydevd.py", line 1658, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "C:\Users\DELL\.IntelliJIdea2018.1\config\plugins\python\helpers\pydev\pydevd.py", line 1068, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Users\DELL\.IntelliJIdea2018.1\config\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "F:/workSpace/agent_idea/client/src/main/py/TaxRateSpider.py", line 12, in <module>
ua = UserAgent(use_cache_server=False)
File "D:\py\lib\site-packages\fake_useragent\fake.py", line 69, in __init__
self.load()
File "D:\py\lib\site-packages\fake_useragent\fake.py", line 78, in load
verify_ssl=self.verify_ssl,
File "D:\py\lib\site-packages\fake_useragent\utils.py", line 250, in load_cached
update(path, use_cache_server=use_cache_server, verify_ssl=verify_ssl)
File "D:\py\lib\site-packages\fake_useragent\utils.py", line 245, in update
write(path, load(use_cache_server=use_cache_server, verify_ssl=verify_ssl))
File "D:\py\lib\site-packages\fake_useragent\utils.py", line 178, in load
raise exc
File "D:\py\lib\site-packages\fake_useragent\utils.py", line 154, in load
for item in get_browsers(verify_ssl=verify_ssl):
File "D:\py\lib\site-packages\fake_useragent\utils.py", line 97, in get_browsers
html = get(settings.BROWSERS_STATS_PAGE, verify_ssl=verify_ssl)
File "D:\py\lib\site-packages\fake_useragent\utils.py", line 84, in get
raise FakeUserAgentError('Maximum amount of retries reached')
fake_useragent.errors.FakeUserAgentError: Maximum amount of retries reached
Process finished with exit code 1
这是我脚本使用 fake_useragent 姿势

解决方法:
1: 下载 fake_useragent.json
链接:https://pan.baidu.com/s/1hVKe476erO9V6_2KAlA8lg
提取码:b4m4
2: 将json文件放到目录下 (这里我放的位置是跟我这个脚本同级目录, 你们也可以看这来, 只要确保下面的代码可以获取到这个json文件即可)
3:修改代码
location = os.getcwd() + '/fake_useragent_0.1.11.json' ua = UserAgent(path=location)
4: 点赞,评论,收藏 一键三连