簡體   English   中英

如何將URL中括號中的文字去掉,並將屬性整理成一個列表?

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

這是我從 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"}']

我如何只提取特征並將它們組織到列表中?

方便的是,您的字符串恰好是字典。 總是這樣嗎? 你可以 map 你的字符串列表到字典列表:

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

您可能會考慮使用json.load讀取coolcat.txt的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM