使用 Web API 和请求函数加载多个数据

Loading multiples data with WebAPI and requests function

我试图在多年、几个月内从 WEBAPI 加载数据。假设我需要从 2010 年 1 月到 2020 年 12 月加载,我该如何编写一个函数来执行它?下面是 url:

api.census.gov/data/2020/cps/basic/jan?get=PEMLR,PWSSWGT,PEMARITL&for=state:01&PEEDUCA=39&key=YOUR_KEY_GOES_HERE

以下是我的粗略想法,但不太确定如何写出来。任何人都可以纠正我正在尝试做的事情吗?

Year = ['2010-2020']
Month = ['jan-dec']

call = "https://api.census.gov/data/{Year}/cps/basic/{Month}?get=PEMLR,PWSSWGT,PEMARITL&for=state:01&PEEDUCA=39&key=YOUR_KEY_GOES_HERE**.format(i['Year'],i['Month')
print(call)

使用带有 2 个循环的列表理解和 f-string 格式化 (Python >= 3.6):

KEY = "my_key"
url = "https://api.census.gov/data/"
years = range(2010, 2021)
months = ["jan", "feb", "mar", "apr"]  # needs completion
calls = [f"{url}/{y}/cps/basic/{m}?get=PEMLR,PWSSWGT,PEMARITL&for=state:01&PEEDUCA=39&key={KEY}" for y in years for m in months]
for call in calls:
    print(call)