带有限价库的公司房屋限价处理

时间:2019-06-05 10:01:44

标签: api python-requests rate-limiting http-status-code-429 exponential-backoff

我正在与companies house API一起提取数千个profiles of companies

正常速率限制为600/5分钟,但可以扩展为1200/5分钟。上周,我使用了此脚本,并且该脚本可以正常工作几个小时,现在我一直遇到429错误。

我认为脚本很好,但是我可能缺少ratelimitbackoff库中的装饰器,也许有人更熟悉这些库,并发现了逻辑错误,但我不是看到。

# api_funcs.py module

import requests
from requests import ConnectionError
from ratelimit import limits, sleep_and_retry
from backoff import on_exception, expo
from pipeline_tools.helpers import get_key    

KEY = get_key("API_key")

FIVE_MINUTES = 300  # Number of seconds in five minutes.


@sleep_and_retry  # if we exceed the ratelimit imposed by @limits forces sleep until we can start again.
@on_exception(expo, ConnectionError, max_tries=5)
@limits(calls=1200, period=FIVE_MINUTES)
def call_api(url, api_key):

    r = requests.get(url, auth=(api_key, ""))

    if not (r.status_code == 200 or r.status_code == 404):
        r.raise_for_status()

    elif r.status_code == 404:
        return dict({"error": "not found"})

    else:
        return r.json()

def company_basic_search(comp_code):
    return call_api(url=API_BASE_URL+"/company/"+comp_code, api_key=KEY)

# [list of 200,000 company codes]
comp_codes = ['XXX1','XXX2','XXX3'] 

for code in comp_codes:
    basic_profile_resource = company_basic_search(comp_code=code)
    # if-elif-else flow inserting the object in error table if 404 or in other table if 200. 

我不断收到

raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 429 Client Error: Too Many Requests for url: ...

我的逻辑是否有问题,或者这可能是在API方面?

1 个答案:

答案 0 :(得分:0)

代码工作正常-这是CH服务器方面的问题,其实际限制低于文档中报告的限制。