首页 python正文

使用随机UserAgent报错Error occurred during loading data. Trying to use cache server https://fake-useragent

启明SEO python 2021-04-29 09:53:07 5956 0 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}


评论

Python好学公众号

好文推荐

实时访客入口页面出现bd_vid=标识是什么原因

实时访客入口页面出现bd_vid=标识是什么原因

最近查看百度实时访客,总是会看到入口页面会多......

有了微信视频号,但是没有发布权限,怎么开通

有了微信视频号,但是没有发布权限,怎么开通

视频号是微信在2020年1月19号开始内测的......

如何用python抓取爱企查企业信息

如何用python抓取爱企查企业信息

前段时间,经理让我去找一些企业的信息,我平常......

服务器配置低,宝塔面板安装IIS总是安装不上怎么办

服务器配置低,宝塔面板安装IIS总是安装不上怎么办

对于一般企业网站或者博客来说,对服务器的配置......

如何修改浏览器指纹,Canvas指纹、Webgl指纹、Audio指纹修改方法?

如何修改浏览器指纹,Canvas指纹、Webgl指纹、Audio指纹修改方法?

修改浏览指纹的方法其实也是很简单,主要使用到......

win10下安装MYSQL后,提示Can't connect to MySQL server on localhost (10061)相关问题解决方法

win10下安装MYSQL后,提示Can't connect to MySQL server on localhost (10061)相关问题解决方法

我遇到的问题是电脑原先安装过mysql,卸......