在python中捕获pytrends.exceptions.ResponseError时出错

时间:2018-11-03 18:31:33

标签: python python-3.x

当他们将您锁定在pytrends的服务器之外时,我正在尝试捕获由Google引发的异常。

这是我得到的确切错误消息:

  File "/usr/local/lib/python3.7/site-packages/pytrends/request.py",     line 101, in _get_data
'response with code {0}.'.format(response.status_code), response=response)
pytrends.exceptions.ResponseError: The request failed: Google returned a response with code 429.

我尝试运行except pytrends.exceptions.ResponseError:,但是当我这样做时,出现属性错误AttributeError: 'TrendReq' object has no attribute 'exceptions'

如果有人知道如何实现这一目标,那就太好了,谢谢!

1 个答案:

答案 0 :(得分:2)

要导入错误:

from pytrends.exceptions import ResponseError

然后您可以抓住它:

try:
    pass  # normal behaviour
except ResponseError:
    pass  # treat your error