chemicaluser
chemicaluser

Reputation: 45

Python - Pass results of request get into another request get

I am a beginner Python user. I am trying to take the results of one request.get and pass the values into another. Essentially (the first response.get) provides the customer ID numbers - and then i am trying to take those customerID numbers and get the customer score (comes from the second response.get)

I got the first portion working, and it is returning the results as expected. I am just not sure how to pass the values into the second piece.

p = (('Location', 'CA'), ('NumRows', '5'))
prepare_link = requests.get('https://api.linktomyapi?', auth=BearerAuth('AMB7i7F63t13s58yN9kLtLNBjo607Di7'), params=p)
test = requests.get(prepare_link.url, auth=BearerAuth('AMB7i7F63t13s58yN9kLtLNBjo607Di7'), params=p)

data = json.loads(test.text)

for customer in data['Data']:        
    BusinessID = customer['Id']
    print(BusinessID)

This provides me the output of

5316636
14210
6509
1305
13249780

I am now trying to take this output and pass the values into another get where i want to output is as follows

Customer ID (from above) - Customer Score (from second get)

p2 = ()
prepare_link2 = requests.get('https://api.linktomyapicustmerscore?', auth=BearerAuth('AMB7i7F63t13s58yN9kLtLNBjo607Di7'), params=p2)

how can I set up a loop to pass the Customer ID values (from first get) into p2 for all 5 rows displayed?

I did try to store results of first get into an array using

  CustomerIdList = []
    for customer in data['Data']:
        #print(customer['Id'])
        
        BusinessID = customer['Id']
        CustomerIdList.append(str(customer['Id']))
    
    print (CustomerIdList)

which provided me this output

['5316636', '14210', '6509', '1305', '13249780']

I just don't know how to use it to create loop for second get.

Upvotes: 1

Views: 64

Answers (2)

Barbaros Özhan
Barbaros Özhan

Reputation: 65218

You can initialize a list and append within a loop :

data = json.loads(test.text) #imported from json library
i=0
mlis=[]
for customer in data:         
      if customer == 'Data':
            BusinessID = data[customer]
            for customer in BusinessID:
                  mlis.append(BusinessID[i]['Id'])
                  i+=1

print(mlis)  

Upvotes: 1

Homer
Homer

Reputation: 429

You can iterate through CustomerIdList list. Example below:

CustomerIdList=['5316636', '14210', '6509', '1305', '13249780']

for i in CustomerIdList:
    print ("requests.get('https://api.linktomyapicustmerscore?', auth=BearerAuth('AMB7i7F63t13s58yN9kLtLNBjo607Di7'), params="+i+")")

Upvotes: 1

Related Questions