如何从括号中删除URL中的文本,并将属性组织到列表中?

2024-05-17 11:14:05 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我从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)

哪个输出:

['{"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"}']

我如何提取特征并将其组织成一个列表


Tags: fromhttpsimageimportcomurlvalueas
1条回答
网友
1楼 · 发布于 2024-05-17 11:14:05

很方便,您的字符串恰好是字典。总是这样吗? 您可以将字符串列表映射到字典列表:

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)

然后打印字典的值

上述示例输出:

[{'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'}]

您可以考虑使用{{CD2>}读取^ {CD1>}的内容。

相关问题 更多 >