UK Companies House API-Python多字符串请求

时间:2018-10-03 05:58:54

标签: python python-requests

我有一个python模块,该模块利用chwrapper软件包来调用Companies House Beta API。我有一个工作函数,一次传递一个字符串并返回一个json响应,然后我将其转换为python字典并将结果映射到我的pandas数据框。

我在下面建立了以下函数,希望可以将整个字符串列表作为搜索词,但是每次我只会得到一个字符串响应。

# Function which calls to the Companies House API via chwrapper, passes in org_string column from df

def get_org_id(df):

    org_id = {}
    s = chwrapper.Search(access_token="***API_KEY***")
    org_strings = df['org_string']

    # Pass entire list of strings 
    r = s.search_companies(org_strings)
    comp_house_dict = r.json()

    # For each org_string in the sub-array of org_strings, pull org data from companies house
    for word in org_strings:

        for i in range(0, len(comp_house_dict['items']), 1):

            org_title = str(comp_house_dict['items'][i]['title'])
            basic_string_comph = org_title.replace("  ", " ")
            basic_string_df = word.replace("  ", " ")
            if basic_string_comph == basic_string_df:
                print("MATCH")
                org_id[word] = comp_house_dict['items'][i]['company_number']
                org_id.update(org_id)

    print(org_id)
    return org_id

例如,当我通过时:

org_strings = ['001 INSPIRATION LIMITED', '007 PEST CONTROL LTD']

返回的字典是:

{'001 INSPIRATION LIMITED': '06589669'}

注意:当我在单个请求中传递第二个字符串时,字典将按预期输出,因此搜索项有效。

这是对API的限制,还是我的功能不正确? 非常感谢。

0 个答案:

没有答案