这是我从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"}']
我如何提取特征并将其组织成一个列表
很方便,您的字符串恰好是字典。总是这样吗? 您可以将字符串列表映射到字典列表:
然后打印字典的值
上述示例输出:
您可以考虑使用{{CD2>}读取^ {CD1>}的内容。
相关问题 更多 >
编程相关推荐