你可以用
sets
查找字典中的项目是否已更改。我使用了
another question
但这在某种程度上可以用来解决你的问题
import requests
import time
def dict_compare(d1, d2):
d1_keys = set(d1.keys())
d2_keys = set(d2.keys())
shared_keys = d1_keys.intersection(d2_keys)
added = d1_keys - d2_keys
removed = d2_keys - d1_keys
modified = {o : (d1[o], d2[o]) for o in shared_keys if d1[o] != d2[o]}
same = set(o for o in shared_keys if d1[o] == d2[o])
return added, removed, modified, same
first = requests.get("https://api-mainnet.magiceden.dev/v2/collections?offset=0&limit=1").json()[0]
while True:
get_second = requests.get("https://api-mainnet.magiceden.dev/v2/collections?offset=0&limit=1")
if get_second.status_code == 200:
print("ok")
second = get_second.json()[0]
added, removed, modified, same = dict_compare(first, second)
if len(added) > 0 or len(modified) > 0 or len(removed) > 0:
print("added: ", added)
print("modified: ", modified)
print("removed: ", removed)
else:
print("No change")
time.sleep(15)