大良建设网站/为什么不建议去外包公司上班
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 getreturn response.read()File "C:\Users\de\AppData\Local\Programs\Python\Python38-32\lib\http\client.py", line 461, in readreturn self._readall_chunked()File "C:\Users\de\AppData\Local\Programs\Python\Python38-32\lib\http\client.py", line 571, in _readall_chunkedvalue.append(self._safe_read(chunk_left))File "C:\Users\de\AppData\Local\Programs\Python\Python38-32\lib\http\client.py", line 608, in _safe_readdata = self.fp.read(amt)File "C:\Users\de\AppData\Local\Programs\Python\Python38-32\lib\socket.py", line 669, in readintoreturn 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 loadbrowsers_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_versionshtml = get(File "C:\Users\de\AppData\Local\Programs\Python\Python38-32\lib\site-packages\fake_useragent\utils.py", line 84, in getraise 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
来自我的个人博客http://www.granet.top/