1

So I have this list which stores different request URLS for finding an account.

accounts= []

Each url looks something like this. 'https://example.somewebsite.ie:000/v12345/accounts/12345/users'

Each URL has a different ID so the next URL in the list would be for example 'https://example.somewebsite.ie:000/v12345/accounts/54321/users'

I've tried doing something like this.


accountReq = []
for i in accounts:
    accountReq = requests.get(i, headers=headers).json()



for each in accountReq['data']:
    print(each['username']+" "+" ",each['first_name'],each['last_name'])


This only prints out the last GET requests data.

How do I write multiple requests based on taking each different value in my list?

I can do separate requests line by line but I don't want to do this as I may have a 100 plus URLS to try.

5
  • What error do you see? Iterating over a list of properly constructed URLs like that is fine Commented May 19, 2022 at 17:52
  • When I save a response , it only saves the response from one of the URLS. I need to save all responses from all URLS @LancelotduLac Commented May 19, 2022 at 17:56
  • You need to show more of your code to make this question relevant. I suspect you need a list (accountReq) to which you should append the responses otherwise accountReq will only ever contain the result of the last GET Commented May 19, 2022 at 17:57
  • sure, I will update now in a sec. Commented May 19, 2022 at 17:58
  • I see you updated your last comment, can you please post a coded example of how creating a list of responses should look like? Commented May 19, 2022 at 18:05

3 Answers 3

1

accountReq only holds the last data because you're redefining the variable on each iteration. You should change your code to this:

accountReq = []
for i in accounts:
    accountReq.append(requests.get(i, headers=headers).json())



for each in accountReq:
    account = each['data']
    for data in account:
        print(data['username']+" "+" ",data['first_name'],data['last_name'])

Now this will ADD every individual JSON object data to accountReq list.

Sign up to request clarification or add additional context in comments.

4 Comments

This throws a ` for each in accountReq['data']: TypeError: list indices must be integers or slices, not str ` error
@mrOlympia Edited the code. I think it should work now.
that didn't work but I tweaked it and it is working! I will post my answer and then accept yours.
I have looked at the code you provided and fixed my answer accordingly to eliminate any confusion for future readers.
1

If you're only interested in printing certain values then:

accounts = [] # a list of URLs

for account in accounts:
  (r := requests.get(account, headers=headers)).raise_for_status()
  for d in r.json()['data']:
    print(f"{d['username']}, {d['first_name']}, {d['last_name']}")

2 Comments

This throw this following error > print(f"{d['username']}, {d['first_name']}, {d['last_name']}") TypeError: list indices must be integers or slices, not str
@mrOlympia That's because r.json()['data'] is returning a list rather than a dictionary so your JSON structure is not as implied by your original code. I will edit my answer to account for that
0
for each in accountReq:
        account = each['data']
        for data in account:
         print(data['username']+" "+" ",data['first_name'],data['last_name'])

that's what I done after tweaking an answer someone provided.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.