How do I remove the text in URL from the brackets, and organize the attributes into a list?

Viewed 55

This is my code to extract the text from the url:

from bs4 import BeautifulSoup
import requests
import pandas as pd
import re
from re import findall

url = 'https://api.coolcatsnft.com/cat/6003'
c = requests.get(url).text
soup = BeautifulSoup(c, 'html.parser')

with open("coolcat.txt", "w") as file:
    file.write(str(soup))

mylines = []                               
with open ('coolcat.txt', 'rt') as myfile: 
    for myline in myfile:                  
        mylines.append(myline)             
print(mylines)

which outputs this:

['{"description":"Cool Cats is a collection of 9,999 randomly generated and stylistically curated NFTs that exist on the Ethereum Blockchain. Cool Cat holders can participate in exclusive events such as NFT claims, raffles, community giveaways, and more. Remember, all cats are cool, but some are cooler than others. Visit [www.coolcatsnft.com](https://www.coolcatsnft.com/) to learn more.","image":"https://ipfs.io/ipfs/QmcgQpxMNRw4Zt5B1tkDZfA4aAWCBhv6JVw4YnLync2x4e","name":"Cool Cat #6003","attributes":[{"trait_type":"body","value":"blue cat skin"},{"trait_type":"hats","value":"headband red"},{"trait_type":"shirt","value":"toga"},{"trait_type":"face","value":"ninja red"},{"trait_type":"tier","value":"cool_2"}],"points":{"Body":0,"Hats":1,"Shirt":2,"Face":1},"ipfs_image":"https://ipfs.io/ipfs/QmcgQpxMNRw4Zt5B1tkDZfA4aAWCBhv6JVw4YnLync2x4e","google_image":"https://drive.google.com/uc?id=1zI7STVzE6sEBPzfjs__ejqphxz9QUhLu"}']

How do I extract just the traits and organize them into a list?

1 Answers

Conveniently, your strings happen to be dictionaries. Is it always the case? You can map your string list to a list of dictionaries:

li = ['{"description":"Cool Cats is a collection of 9,999 randomly generated and stylistically curated NFTs that exist on the Ethereum Blockchain. Cool Cat holders can participate in exclusive events such as NFT claims, raffles, community giveaways, and more. Remember, all cats are cool, but some are cooler than others. Visit [www.coolcatsnft.com](https://www.coolcatsnft.com/) to learn more.","image":"https://ipfs.io/ipfs/QmcgQpxMNRw4Zt5B1tkDZfA4aAWCBhv6JVw4YnLync2x4e","name":"Cool Cat #6003","attributes":[{"trait_type":"body","value":"blue cat skin"},{"trait_type":"hats","value":"headband red"},{"trait_type":"shirt","value":"toga"},{"trait_type":"face","value":"ninja red"},{"trait_type":"tier","value":"cool_2"}],"points":{"Body":0,"Hats":1,"Shirt":2,"Face":1},"ipfs_image":"https://ipfs.io/ipfs/QmcgQpxMNRw4Zt5B1tkDZfA4aAWCBhv6JVw4YnLync2x4e","google_image":"https://drive.google.com/uc?id=1zI7STVzE6sEBPzfjs__ejqphxz9QUhLu"}']
dic_list = list(map(eval, li))

for dic in dic_list:
    traits = dic["attributes"]
    print(traits)

You then print the values of the dictionaries.

The above example outputs:

[{'trait_type': 'body', 'value': 'blue cat skin'}, {'trait_type': 'hats', 'value': 'headband red'}, {'trait_type': 'shirt', 'value': 'toga'}, {'trait_type': 'face', 'value': 'ninja red'}, {'trait_type': 'tier', 'value': 'cool_2'}]

You might consider reading the content of coolcat.txt with json.load though.

Related