使用随机UserAgent报错Error occurred during loading data. Trying to use cache server https://fake-useragent
今天在使用随机请求头运行程序后报错了,我的代码是这样的:
ua = UserAgent() useragent=ua.firefox headers={'user-agent':useragent}
报错信息是这样的:
Error occurred during loading data. Trying to use cache server https://fake-useragent.herokuapp.com/browsers/0.1.11 Traceback (most recent call last): File "D:\Python37\lib\urllib\request.py", line 1317, in do_open encode_chunked=req.has_header('Transfer-encoding')) File "D:\Python37\lib\http\client.py", line 1244, in request self._send_request(method, url, body, headers, encode_chunked) File "D:\Python37\lib\http\client.py", line 1290, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "D:\Python37\lib\http\client.py", line 1239, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "D:\Python37\lib\http\client.py", line 1026, in _send_output self.send(msg) File "D:\Python37\lib\http\client.py", line 966, in send self.connect() File "D:\Python37\lib\http\client.py", line 1406, in connect super().connect() File "D:\Python37\lib\http\client.py", line 938, in connect (self.host,self.port), self.timeout, self.source_address) File "D:\Python37\lib\socket.py", line 727, in create_connection raise err File "D:\Python37\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:\Python37\lib\site-packages\fake_useragent\utils.py", line 67, in get context=context, File "D:\Python37\lib\urllib\request.py", line 222, in urlopen return opener.open(url, data, timeout) File "D:\Python37\lib\urllib\request.py", line 525, in open response = self._open(req, data) File "D:\Python37\lib\urllib\request.py", line 543, in _open '_open', req) File "D:\Python37\lib\urllib\request.py", line 503, in _call_chain result = func(*args) File "D:\Python37\lib\urllib\request.py", line 1360, in https_open context=self._context, check_hostname=self._check_hostname) File "D:\Python37\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 "D:\Python37\lib\site-packages\fake_useragent\utils.py", line 154, in load for item in get_browsers(verify_ssl=verify_ssl): File "D:\Python37\lib\site-packages\fake_useragent\utils.py", line 97, in get_browsers html = get(settings.BROWSERS_STATS_PAGE, verify_ssl=verify_ssl) File "D:\Python37\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
找了相关资料后,找到了解决方法,修改后正确运行的代码如下:
ua = UserAgent(use_cache_server=False) useragent=ua.firefox headers={'user-agent':useragent}
评论