Home > front end >  Iterating two components in URLs
Iterating two components in URLs

Time:07-15

I'm trying to get an dataframe for each collection with information of every token in the collection.

So what I tried to do is to put one collection's address and having it fixed through the every token_id iteration and get the list of 'traits' information and rebuild the list of dictionaries using 'trait_type' as a key and 'value' as a value for every dictionary in my response.

collections =["0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D","0x8a90cab2b38dba80c64b7734e58ee1db38b8992e"]
token_id = range(1,10)

traits_responses= []
    
        
for t_id,address in zip(token_id, collections) :
            url=f"https://api.opensea.io/api/v1/asset/{address}/{t_id}/?account_address={address}&include_orders=false"
            headers = {"X-API-KEY": "api_key"}
            response = requests.get(url, headers=headers)
            response_json = response.json()['traits']
            traits_responses.append(response_json)

collection_traits = pd.DataFrame([{x['trait_type']:x['value'] for x in d} for d in traits_responses])
collection_traits

But the result of the above code turns out to be,

Background  Fur Clothes Mouth   Eyes    background  face    head    body    hair
0   Orange  Robot   Vietnam Jacket  Grin    Blue Beams  NaN NaN NaN NaN NaN
1   NaN NaN NaN NaN NaN yellow  designer glasses    purple  blue fleece poopie

the 'trait responses' are as below,

[[{'trait_type': 'Background',
   'value': 'Red',
   'display_type': None,
   'max_value': None,
   'trait_count': 1006,
   'order': None},
  {'trait_type': 'Mouth',
   'value': 'Relaxed',
   'display_type': None,
   'max_value': None,
   'trait_count': 834,
   'order': None},
  {'trait_type': 'Type',
   'value': 'Red',
   'display_type': None,
   'max_value': None,
   'trait_count': 441,
   'order': None},
  {'trait_type': 'Offhand',
   'value': 'Sake',
   'display_type': None,
   'max_value': None,
   'trait_count': 155,
   'order': None},
  {'trait_type': 'Eyes',
   'value': 'Focused',
   'display_type': None,
   'max_value': None,
   'trait_count': 338,
   'order': None},
  {'trait_type': 'Clothing',
   'value': 'Azuki Track Jacket',
   'display_type': None,
   'max_value': None,
   'trait_count': 157,
   'order': None},
  {'trait_type': 'Hair',
   'value': 'Magenta Messy',
   'display_type': None,
   'max_value': None,
   'trait_count': 86,
   'order': None}],
 [{'trait_type': 'Background',
   'value': 'Red',
   'display_type': None,
   'max_value': None,
   'trait_count': 1006,
   'order': None},
  {'trait_type': 'Mouth',
   'value': 'Relaxed',
   'display_type': None,
   'max_value': None,
   'trait_count': 834,
   'order': None},
  {'trait_type': 'Type',
   'value': 'Red',
   'display_type': None,
   'max_value': None,
   'trait_count': 441,
   'order': None},
  {'trait_type': 'Offhand',
   'value': 'Sake',
   'display_type': None,
   'max_value': None,
   'trait_count': 155,
   'order': None},
  {'trait_type': 'Eyes',
   'value': 'Focused',
   'display_type': None,
   'max_value': None,
   'trait_count': 338,
   'order': None},
  {'trait_type': 'Clothing',
   'value': 'Azuki Track Jacket',
   'display_type': None,
   'max_value': None,
   'trait_count': 157,
   'order': None},
  {'trait_type': 'Hair',
   'value': 'Magenta Messy',
   'display_type': None,
   'max_value': None,
   'trait_count': 86,
   'order': None}],
 [{'trait_type': 'Background',
   'value': 'Red',
   'display_type': None,
   'max_value': None,
   'trait_count': 1006,
   'order': None},
  {'trait_type': 'Mouth',
   'value': 'Relaxed',
   'display_type': None,
   'max_value': None,
   'trait_count': 834,
   'order': None},
  {'trait_type': 'Type',
   'value': 'Red',
   'display_type': None,
   'max_value': None,
   'trait_count': 441,
   'order': None},
  {'trait_type': 'Offhand',
   'value': 'Sake',
   'display_type': None,
   'max_value': None,
   'trait_count': 155,
   'order': None},
  {'trait_type': 'Eyes',
   'value': 'Focused',
   'display_type': None,
   'max_value': None,
   'trait_count': 338,
   'order': None},
  {'trait_type': 'Clothing',
   'value': 'Azuki Track Jacket',
   'display_type': None,
   'max_value': None,
   'trait_count': 157,
   'order': None},
  {'trait_type': 'Hair',
   'value': 'Magenta Messy',
   'display_type': None,
   'max_value': None,
   'trait_count': 86,
   'order': None}],
 [{'trait_type': 'Background',
   'value': 'Red',
   'display_type': None,
   'max_value': None,
   'trait_count': 1006,
   'order': None},
  {'trait_type': 'Mouth',
   'value': 'Relaxed',
   'display_type': None,
   'max_value': None,
   'trait_count': 834,
   'order': None},
  {'trait_type': 'Type',
   'value': 'Red',
   'display_type': None,
   'max_value': None,
   'trait_count': 441,
   'order': None},
  {'trait_type': 'Offhand',
   'value': 'Sake',
   'display_type': None,
   'max_value': None,
   'trait_count': 155,
   'order': None},
  {'trait_type': 'Eyes',
   'value': 'Focused',
   'display_type': None,
   'max_value': None,
   'trait_count': 338,
   'order': None},
  {'trait_type': 'Clothing',
   'value': 'Azuki Track Jacket',
   'display_type': None,
   'max_value': None,
   'trait_count': 157,
   'order': None},
  {'trait_type': 'Hair',
   'value': 'Magenta Messy',
   'display_type': None,
   'max_value': None,
   'trait_count': 86,
   'order': None}],
 [{'trait_type': 'Background',
   'value': 'Red',
   'display_type': None,
   'max_value': None,
   'trait_count': 1006,
   'order': None},
  {'trait_type': 'Mouth',
   'value': 'Relaxed',
   'display_type': None,
   'max_value': None,
   'trait_count': 834,
   'order': None},
  {'trait_type': 'Type',
   'value': 'Red',
   'display_type': None,
   'max_value': None,
   'trait_count': 441,
   'order': None},
  {'trait_type': 'Offhand',
   'value': 'Sake',
   'display_type': None,
   'max_value': None,
   'trait_count': 155,
   'order': None},
  {'trait_type': 'Eyes',
   'value': 'Focused',
   'display_type': None,
   'max_value': None,
   'trait_count': 338,
   'order': None},
  {'trait_type': 'Clothing',
   'value': 'Azuki Track Jacket',
   'display_type': None,
   'max_value': None,
   'trait_count': 157,
   'order': None},
  {'trait_type': 'Hair',
   'value': 'Magenta Messy',
   'display_type': None,
   'max_value': None,
   'trait_count': 86,
   'order': None}],
 [{'trait_type': 'Background',
   'value': 'Red',
   'display_type': None,
   'max_value': None,
   'trait_count': 1006,
   'order': None},
  {'trait_type': 'Mouth',
   'value': 'Relaxed',
   'display_type': None,
   'max_value': None,
   'trait_count': 834,
   'order': None},
  {'trait_type': 'Type',
   'value': 'Red',
   'display_type': None,
   'max_value': None,
   'trait_count': 441,
   'order': None},
  {'trait_type': 'Offhand',
   'value': 'Sake',
   'display_type': None,
   'max_value': None,
   'trait_count': 155,
   'order': None},
  {'trait_type': 'Eyes',
   'value': 'Focused',
   'display_type': None,
   'max_value': None,
   'trait_count': 338,
   'order': None},
  {'trait_type': 'Clothing',
   'value': 'Azuki Track Jacket',
   'display_type': None,
   'max_value': None,
   'trait_count': 157,
   'order': None},
  {'trait_type': 'Hair',
   'value': 'Magenta Messy',
   'display_type': None,
   'max_value': None,
   'trait_count': 86,
   'order': None}],
 [{'trait_type': 'Background',
   'value': 'Red',
   'display_type': None,
   'max_value': None,
   'trait_count': 1006,
   'order': None},
  {'trait_type': 'Mouth',
   'value': 'Relaxed',
   'display_type': None,
   'max_value': None,
   'trait_count': 834,
   'order': None},
  {'trait_type': 'Type',
   'value': 'Red',
   'display_type': None,
   'max_value': None,
   'trait_count': 441,
   'order': None},
  {'trait_type': 'Offhand',
   'value': 'Sake',
   'display_type': None,
   'max_value': None,
   'trait_count': 155,
   'order': None},
  {'trait_type': 'Eyes',
   'value': 'Focused',
   'display_type': None,
   'max_value': None,
   'trait_count': 338,
   'order': None},
  {'trait_type': 'Clothing',
   'value': 'Azuki Track Jacket',
   'display_type': None,
   'max_value': None,
   'trait_count': 157,
   'order': None},
  {'trait_type': 'Hair',
   'value': 'Magenta Messy',
   'display_type': None,
   'max_value': None,
   'trait_count': 86,
   'order': None}],
 [{'trait_type': 'Background',
   'value': 'Red',
   'display_type': None,
   'max_value': None,
   'trait_count': 1006,
   'order': None},
  {'trait_type': 'Mouth',
   'value': 'Relaxed',
   'display_type': None,
   'max_value': None,
   'trait_count': 834,
   'order': None},
  {'trait_type': 'Type',
   'value': 'Red',
   'display_type': None,
   'max_value': None,
   'trait_count': 441,
   'order': None},
  {'trait_type': 'Offhand',
   'value': 'Sake',
   'display_type': None,
   'max_value': None,
   'trait_count': 155,
   'order': None},
  {'trait_type': 'Eyes',
   'value': 'Focused',
   'display_type': None,
   'max_value': None,
   'trait_count': 338,
   'order': None},
  {'trait_type': 'Clothing',
   'value': 'Azuki Track Jacket',
   'display_type': None,
   'max_value': None,
   'trait_count': 157,
   'order': None},
  {'trait_type': 'Hair',
   'value': 'Magenta Messy',
   'display_type': None,
   'max_value': None,
   'trait_count': 86,
   'order': None}],
 [{'trait_type': 'Background',
   'value': 'Red',
   'display_type': None,
   'max_value': None,
   'trait_count': 1006,
   'order': None},
  {'trait_type': 'Mouth',
   'value': 'Relaxed',
   'display_type': None,
   'max_value': None,
   'trait_count': 834,
   'order': None},
  {'trait_type': 'Type',
   'value': 'Red',
   'display_type': None,
   'max_value': None,
   'trait_count': 441,
   'order': None},
  {'trait_type': 'Offhand',
   'value': 'Sake',
   'display_type': None,
   'max_value': None,
   'trait_count': 155,
   'order': None},
  {'trait_type': 'Eyes',
   'value': 'Focused',
   'display_type': None,
   'max_value': None,
   'trait_count': 338,
   'order': None},
  {'trait_type': 'Clothing',
   'value': 'Azuki Track Jacket',
   'display_type': None,
   'max_value': None,
   'trait_count': 157,
   'order': None},
  {'trait_type': 'Hair',
   'value': 'Magenta Messy',
   'display_type': None,
   'max_value': None,
   'trait_count': 86,
   'order': None}]]

and I need to make dataframe for each collection like this

Type    Background  Mouth   Eyes    Hair    Clothing    Offhand Ear Neck    Headgear    Face    Special
0   Human   Off White D Lipstick    Daydreaming Pink Hairband   White Qipao with Fur    Gloves  NaN NaN NaN NaN NaN
1   Human   Red Chewing Ruby    Pink Flowy  Vest    NaN Red Tassel  NaN NaN NaN NaN
2   Human   Red Grass   Careless    Green Spiky Green Yukata    Katana  NaN Frog Headphones Frog Headband   NaN NaN
3   Human   Off White D Smirk   Lightning   Brown Dreadlocks    White Qipao with Fur    Katana  NaN NaN NaN NaN NaN
4   Human   Red Chuckle Suspicious  Blonde Swept Back   Red Perfecto Jacket Leather Katana  NaN NaN NaN Red Stripes Face Paint  NaN
... ... ... ... ... ... ... ... ... ... ... ... ...
94  Human   Off White D Smirk   Concerned   Brown Bangs Blue Qipao  NaN NaN NaN NaN NaN NaN
95  Human   Off White C Gaiter  Pensive Brown Spiky White Hoodie    Zanbato NaN NaN NaN NaN NaN
96  Human   Dark Blue   Toothpick   Closed  Silver Swept Back   Red Panda T-Shirt   Zanbato NaN NaN NaN NaN NaN
97  Human   Dark Purple Face Mask   Determined  Blonde Short Spiky  Fur Hoodie  Coin    NaN NaN NaN NaN NaN
98  Red Red Relaxed Focused Magenta Messy   Azuki Track Jacket  Sake    NaN NaN NaN NaN NaN

Any help would be greatly appreciated!

CodePudding user response:

There should be a nested loop here. zip() is useful when iterating two iterables together, which is not appropriate here. As a nested loop is used, there should be a nested list to collect the data and the final output should also use a loop to produce 2 dfs.

collections =["0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D","0x8a90cab2b38dba80c64b7734e58ee1db38b8992e"]
token_id = range(1,11)

traits_responses= []
for address in collections:
    lst = []
    for t_id in token_id:
        url=f"https://api.opensea.io/api/v1/asset/{address}/{t_id}/?account_address={address}&include_orders=false"
        headers = {"X-API-KEY": "api_key"}
        response = requests.get(url, headers=headers)
        response_json = response.json()['traits']
        lst.append(response_json)
    # nested list
    traits_responses.append(lst)

# loop here
collection_traits_lst = [pd.DataFrame([{x['trait_type']:x['value'] for x in d} for d in lst]) for lst in traits_responses]
  • Related