NoneType 不可订阅

NoneType not subscriptable

有人知道为什么会返回“类型错误:'NoneType' 对象不可订阅”吗?

import requests, json


url2 = "https://xxx.xxxxxx.xx/xxx/xx/xxxxxx"
querystring = {"order_direction":"desc","offset":"0","limit":"5000", "owner": "xxxxxxxxxxxxxxxxxxxxxxxx"}
headers = {
    "Accept": "application/json",
    }

r = requests.get(url2, params=querystring, headers=headers).json()
for asset in r['assets']:
    if asset != None:
            print(asset['creator']['user']['username'])

我去和运行一样的代码,问题是你的资产['creator']['user']之一是None

{'user': None, 'profile_img_url': 'https://storage.googleapis.com/opensea-static/opensea-profile/14.png', 'address': '0x283af0b28c62c092c9727f1ee09c02ca627eb7f5', 'config': '', 'discord_id': ''}

因此,当您尝试 运行 print(asset['creator']['user']['username']) 时,您尝试打印 None['user'] 这当然是 returns 一个错误。要修复它,请将您的代码更改为-

for asset in r['assets']:
    if asset != None:
        if asset['creator'] != None:
            if asset['creator']['user'] != None:
                print(asset['creator']['user']['username'])