How loop multiple requests and append in a list

Question:

how can i do a loop of requests with id range 1 until 10 and storage them with append in a list for each result?
Example

 r1 = requests.get("https://test.net/api?id=1") # orange
 r2 = request.get("https://test.net/api?id=2")... # apple 
rn= request.get("https://test.net/api?id=n") # banana

final result
list = [ orange, apple, ... , banana]`
Asked By: Ítalo Lima

||

Answers:

import requests

result = []
for i in range(1, 11):
    resp = requests.get(f"https://test.net/api?id={i}")
    result.append(resp)  # resp.text for HTML content
print(result)

The response will be of type <class 'requests.models.Response'>, so you probably need to do resp.text.

Answered By: sourin
list = []
for x in range(10):
    list.append(requests.get("https://test.net/api?id=" + str(x))
    

If your ids are one-based instead of zero-based you should do range(1,11) instead.

Answered By: spiderduckpig
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.