Python 3使用fake_useragentモジュールのエラーと解決方法


Python3 fake_useragentモジュールの使用とエラー解決
  • 問題発見
  • 解決策
  • 次のコードクリップは、コンソールでテストされます.
    問題を発見する
    
    from fake_useragent import UserAgent
    ra = 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 "C:\Users\de\AppData\Local\Programs\Python\Python38-32\lib\site-packages\fake_useragent\utils.py", line 69, in get
        return response.read()
      File "C:\Users\de\AppData\Local\Programs\Python\Python38-32\lib\http\client.py", line 461, in read
        return self._readall_chunked()
      File "C:\Users\de\AppData\Local\Programs\Python\Python38-32\lib\http\client.py", line 571, in _readall_chunked
        value.append(self._safe_read(chunk_left))
      File "C:\Users\de\AppData\Local\Programs\Python\Python38-32\lib\http\client.py", line 608, in _safe_read
        data = self.fp.read(amt)
      File "C:\Users\de\AppData\Local\Programs\Python\Python38-32\lib\socket.py", line 669, in readinto
        return self._sock.recv_into(b)
    socket.timeout: timed out
    During handling of the above exception, another exception occurred:
    Traceback (most recent call last):
      File "C:\Users\de\AppData\Local\Programs\Python\Python38-32\lib\site-packages\fake_useragent\utils.py", line 164, in load
        browsers_dict[browser_key] = get_browser_versions(
      File "C:\Users\de\AppData\Local\Programs\Python\Python38-32\lib\site-packages\fake_useragent\utils.py", line 120, in get_browser_versions
        html = get(
      File "C:\Users\de\AppData\Local\Programs\Python\Python38-32\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
    
    

    解決策
    from fake_useragent import UserAgent
    ua = UserAgent(use_cache_server=False)
    ua = UserAgent()
    
    

    最後に結果を出力できます
    from fake_useragent import UserAgent
    ua = UserAgent(use_cache_server=False)
    ua = UserAgent()
    print(ua.chrome)
    Mozilla/5.0 (X11; NetBSD) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36
    print(ua.chrome)
    Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.67 Safari/537.36