Home > Mobile >  python multiple requests and append
python multiple requests and append

Time:10-09

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]`

CodePudding user response:

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.

CodePudding user response:

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.

  • Related