Python API 尽管秒数增加,但仍超出限制
Python API limit exceeded despite increasing amount of second
我正在尝试学习 python api 的新闻来源,所以我从这里复制了 python 笔记本代码:source
当我 运行 代码时,除了 api 键之外,我看到了下面的错误(引用限制为 10)。即使我从 6 延长到 30 秒,它也会出现。我不确定为什么会出现此错误,因为它没有出现在代码创建者(他们的整个回复已发布)中。对不起,我的愚蠢问题,我是新手。
This is my error message:
Working on ['2015-01-01', '00:00:00']...
Working on ['2015-02-01', '00:00:00']...
Working on ['2015-03-01', '00:00:00']...
Working on ['2015-04-01', '00:00:00']...
Working on ['2015-05-01', '00:00:00']...
Working on ['2015-06-01', '00:00:00']...
Working on ['2015-07-01', '00:00:00']...
Working on ['2015-08-01', '00:00:00']...
Working on ['2015-09-01', '00:00:00']...
Working on ['2015-10-01', '00:00:00']...
Working on ['2015-11-01', '00:00:00']... <-- Always stops here
in parse_response
{'fault': {'faultstring': 'Rate limit quota violation. Quota limit exceeded. 'detail': {'errorcode': 'policies.ratelimit.QuotaViolation'}}}
{'headline': [], 'date': [], 'doc_type': [], 'material_type': [], 'section': [], 'keywords': []}
url比较:
在纽约工作 api:
https://api.nytimes.com/svc/archive/v1/2019/1.json?api-key=YOUR_API_KEY
生成的代码 url 表示访问被拒绝,即使我可以访问 NYT 存档:
https://api.nytimes.com/svc/archive/v1/2016-01-01/00:00:00.json?api-key=YOUR_API_KEY
'''fixed url new error message when running from python'''
超出 IOPub 数据速率。
笔记本服务器将暂时停止发送输出
给客户端,以避免崩溃。
要更改此限制,请设置配置变量
--NotebookApp.iopub_data_rate_limit
.
当前值:
NotebookApp.iopub_data_rate_limit=1000000.0 (bytes/sec)
NotebookApp.rate_limit_window=3.0(秒)
但在浏览器中有效:https://api.nytimes.com/svc/archive/v1/2016/1.json?api-key=YOUR_API_KEY
我看到其他人用google问这个问题,但这是纽约时报。
谢谢
无论是否引发 Exception
,请确保您等待:
def send_request(date):
'''Sends a request to the NYT Archive API for given date.'''
base_url = 'https://api.nytimes.com/svc/archive/v1/'
url = base_url + '/' + date[0] + '/' + date[1] + '.json?api-key=' + YOUR_API_KEY
try:
response = requests.get(url, verify=False).json()
except Exception:
return None
finally:
time.sleep(6)
return response
finally
子句是在 try
或 except
块完成之前执行的最后一个代码块。这意味着 finally
块将在 try
、except
或 else
(此处不适用)块中的任何 return
语句之前执行。这可能非常令人困惑。我建议在函数末尾放置一个 return
语句,如下所示:
def send_request(date):
'''Sends a request to the NYT Archive API for given date.'''
base_url = 'https://api.nytimes.com/svc/archive/v1/'
url = base_url + '/' + date[0] + '/' + date[1] + '.json?api-key=' + YOUR_API_KEY
try:
response = requests.get(url, verify=False).json()
except Exception:
response = None
finally:
time.sleep(6)
return response
两者都会有相同的结果:无论响应如何都休眠六秒,return 要么是 Response
要么是 None
,但我认为第二个更容易理解。现在,您仍然需要弄清楚为什么会出现 Exception
并修复它。我建议在 except
块中添加一些 print()
调试语句以弄清楚到底发生了什么。
另一种选择是假设 reader 完全理解 Python 的 try-except-else-finally 行为:
def send_request(date):
'''Sends a request to the NYT Archive API for given date.'''
base_url = 'https://api.nytimes.com/svc/archive/v1/'
url = base_url + '/' + date[0] + '/' + date[1] + '.json?api-key=' + YOUR_API_KEY
try:
return requests.get(url, verify=False).json()
except Exception:
return None
finally:
time.sleep(6)
这个 article 很好地解释了 Python 的 try-except-else-finally 行为。
我正在尝试学习 python api 的新闻来源,所以我从这里复制了 python 笔记本代码:source
当我 运行 代码时,除了 api 键之外,我看到了下面的错误(引用限制为 10)。即使我从 6 延长到 30 秒,它也会出现。我不确定为什么会出现此错误,因为它没有出现在代码创建者(他们的整个回复已发布)中。对不起,我的愚蠢问题,我是新手。
This is my error message:
Working on ['2015-01-01', '00:00:00']...
Working on ['2015-02-01', '00:00:00']...
Working on ['2015-03-01', '00:00:00']...
Working on ['2015-04-01', '00:00:00']...
Working on ['2015-05-01', '00:00:00']...
Working on ['2015-06-01', '00:00:00']...
Working on ['2015-07-01', '00:00:00']...
Working on ['2015-08-01', '00:00:00']...
Working on ['2015-09-01', '00:00:00']...
Working on ['2015-10-01', '00:00:00']...
Working on ['2015-11-01', '00:00:00']... <-- Always stops here
in parse_response
{'fault': {'faultstring': 'Rate limit quota violation. Quota limit exceeded. 'detail': {'errorcode': 'policies.ratelimit.QuotaViolation'}}}
{'headline': [], 'date': [], 'doc_type': [], 'material_type': [], 'section': [], 'keywords': []}
url比较:
在纽约工作 api: https://api.nytimes.com/svc/archive/v1/2019/1.json?api-key=YOUR_API_KEY
生成的代码 url 表示访问被拒绝,即使我可以访问 NYT 存档:
https://api.nytimes.com/svc/archive/v1/2016-01-01/00:00:00.json?api-key=YOUR_API_KEY
'''fixed url new error message when running from python'''
超出 IOPub 数据速率。
笔记本服务器将暂时停止发送输出
给客户端,以避免崩溃。
要更改此限制,请设置配置变量
--NotebookApp.iopub_data_rate_limit
.
当前值: NotebookApp.iopub_data_rate_limit=1000000.0 (bytes/sec) NotebookApp.rate_limit_window=3.0(秒)
但在浏览器中有效:https://api.nytimes.com/svc/archive/v1/2016/1.json?api-key=YOUR_API_KEY
我看到其他人用google问这个问题,但这是纽约时报。
谢谢
无论是否引发 Exception
,请确保您等待:
def send_request(date):
'''Sends a request to the NYT Archive API for given date.'''
base_url = 'https://api.nytimes.com/svc/archive/v1/'
url = base_url + '/' + date[0] + '/' + date[1] + '.json?api-key=' + YOUR_API_KEY
try:
response = requests.get(url, verify=False).json()
except Exception:
return None
finally:
time.sleep(6)
return response
finally
子句是在 try
或 except
块完成之前执行的最后一个代码块。这意味着 finally
块将在 try
、except
或 else
(此处不适用)块中的任何 return
语句之前执行。这可能非常令人困惑。我建议在函数末尾放置一个 return
语句,如下所示:
def send_request(date):
'''Sends a request to the NYT Archive API for given date.'''
base_url = 'https://api.nytimes.com/svc/archive/v1/'
url = base_url + '/' + date[0] + '/' + date[1] + '.json?api-key=' + YOUR_API_KEY
try:
response = requests.get(url, verify=False).json()
except Exception:
response = None
finally:
time.sleep(6)
return response
两者都会有相同的结果:无论响应如何都休眠六秒,return 要么是 Response
要么是 None
,但我认为第二个更容易理解。现在,您仍然需要弄清楚为什么会出现 Exception
并修复它。我建议在 except
块中添加一些 print()
调试语句以弄清楚到底发生了什么。
另一种选择是假设 reader 完全理解 Python 的 try-except-else-finally 行为:
def send_request(date):
'''Sends a request to the NYT Archive API for given date.'''
base_url = 'https://api.nytimes.com/svc/archive/v1/'
url = base_url + '/' + date[0] + '/' + date[1] + '.json?api-key=' + YOUR_API_KEY
try:
return requests.get(url, verify=False).json()
except Exception:
return None
finally:
time.sleep(6)
这个 article 很好地解释了 Python 的 try-except-else-finally 行为。