Python3 使用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

来自我的个人博客http://www.granet.top/

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值