Home > Back-end >  Looping through different pandas series for request
Looping through different pandas series for request

Time:03-04

An api requires two different parameters, so I want to loop through those parameters, which are stored in pandas series, so I want a way to better loop through them, so they get requested at the same time.

This is the API request:

url = f"https://api.opensea.io/api/v1/asset/a/b/listings?limit=50"
headers = {
    "Accept": "application/json",
    "X-API-KEY": "becbd5981e0c482083b3bac4267ad651"
}
response = requests.request("GET", url, headers=headers)
response

a and b refer to the parameters I want to insert.

a           b
5355        0x2efa07cac3395599db83035d196f2a0e7263f766
22591       0x60e4d786628fea6478f785a6d7e704777c86a7c6
165         0x3a5051566b2241285be871f650c445a88a970edd
12971       0x9ca8887d13bc4591ae36972702fdf9de2c97957f
6706        0x521f9c7505005cfa19a8e5786a9c3c9c9f5e6f42
6230        0x8a90cab2b38dba80c64b7734e58ee1db38b8992e
2746        0xc7df86762ba83f2a6197e1ff9bb40ae0f696b9e6
803         0x177ef8787ceb5d4596b6f011df08c86eb84380dc
5682        0x2acab3dea77832c09420663b0e1cb386031ba17b
7473        0xba30e5f9bb24caa003e9f2f0497ad287fdf95623

So I want a way to loop through both a and b to make the request, such that each row of a and b are requested together, and it doesn't mix up.

CodePudding user response:

Use iterrows:

# Remove the f-strings
url = "https://api.opensea.io/api/v1/asset/{a}/{b}/listings?limit=50"

headers = {
    "Accept": "application/json",
    "X-API-KEY": "becbd5981e0c482083b3bac4267ad651"
}

for _, row in df.iterrows():
  # response = requests.request("GET", url.format(**row.to_dict()), headers=headers)
  # print(response)
  print(url.format(**row.to_dict()))

Output:

https://api.opensea.io/api/v1/asset/5355/0x2efa07cac3395599db83035d196f2a0e7263f766/listings?limit=50
https://api.opensea.io/api/v1/asset/22591/0x60e4d786628fea6478f785a6d7e704777c86a7c6/listings?limit=50
https://api.opensea.io/api/v1/asset/165/0x3a5051566b2241285be871f650c445a88a970edd/listings?limit=50
https://api.opensea.io/api/v1/asset/12971/0x9ca8887d13bc4591ae36972702fdf9de2c97957f/listings?limit=50
https://api.opensea.io/api/v1/asset/6706/0x521f9c7505005cfa19a8e5786a9c3c9c9f5e6f42/listings?limit=50
https://api.opensea.io/api/v1/asset/6230/0x8a90cab2b38dba80c64b7734e58ee1db38b8992e/listings?limit=50
https://api.opensea.io/api/v1/asset/2746/0xc7df86762ba83f2a6197e1ff9bb40ae0f696b9e6/listings?limit=50
https://api.opensea.io/api/v1/asset/803/0x177ef8787ceb5d4596b6f011df08c86eb84380dc/listings?limit=50
https://api.opensea.io/api/v1/asset/5682/0x2acab3dea77832c09420663b0e1cb386031ba17b/listings?limit=50
https://api.opensea.io/api/v1/asset/7473/0xba30e5f9bb24caa003e9f2f0497ad287fdf95623/listings?limit=50
  • Related