Home > Enterprise >  Loading multiples data with WebAPI and requests function
Loading multiples data with WebAPI and requests function

Time:04-24

I am trying to load the data from WEBAPI in multiple years, months. Say I need to load from Jan-Dec 2010-2020, how to I write a function to execute that? Below is the url:

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

Below is my rough idea, but not too sure how to write them out. Can anyone correct what I am trying to do?

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)

CodePudding user response:

Using list comprehension with 2 loops and f-string formating (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)
  • Related