将两个列表传递到 get 请求中,但它传递了整个列表

Passing two lists into get requests, but it passes entire lists

如何根据两个列表调用 api?
有两个列表,statescounties。使用这些列表调用和获取 some_results。但是 states 列表中的 AK 项没有 counties 作为响应,因此它应该跳过它。但是当我尝试使用以下方式调试它时:

print("requests.get('https://represent.opennorth.ca/states/{0}/counties/{1}/area_codes'.format(states, counties))")

我注意到循环插入了整个列表而不是一个一个地插入列表:

>> requests.get('https://represent.opennorth.ca/states/[AK, GA, NY]/counties/[gwinneth, duluth, manhattan, bronx]/area_codes'

我该如何解决?

states = [AK, GA, NY]
counties = [gwinneth, duluth, manhattan, bronx]

some_results = []
for county in counties:
    rr = requests.get('https://represent.opennorth.ca/states/{0}/counties/{1}/area_codes'.format(states, counties))
    if rr.status_code == 200:
        some_results.append(rr.json())
    else: 
        print("Request to {} failed".format(states, counties))

请尝试此代码。还看了评论。

import requests

# suppose these are all string variables defined elsewhere
states = [AK, GA, NY]  # why not [GA, NY]?
counties = [gwinneth, duluth, manhattan, bronx]

some_results = []
for state in states:
    for county in counties:
        # you should only pass single string here, never list
        url = f'https://represent.opennorth.ca/states/{state}/counties/{county}/area_codes'
        resp = requests.get(url)
        if resp.status_code == 200:
            some_results.append(resp.json())
        else:
            print(f"Request to {state} {county} failed")
            print(resp.status_code)
            print(resp.reason)
            print(resp.text)