它给了我一个错误,我不知道为什么[robloxapi]

问题描述 投票:0回答:1

我不确定为什么它会给我一个错误,但这是脚本,我想知道为什么它会给我一个错误以及如何解决它。

脚本:

import requests
end = []

usernames = ['roblox']
username = usernames.pop(0)
u = str(username) 
r = requests.get(f'https://www.roblox.com/user.aspx?username={username}').url
if 'www.roblox.com/users/' in r:
        userid = r.split('/')[-2]
        id = [1700848132]
        b = requests.post(f'https://presence.roblox.com/v1/presence/userpresencetype', json={'userId': id}).json()[0]
        print(f'{b}:{u}')

错误:

line 11, in <module>
    b = requests.post(f'https://presence.roblox.com/v1/presence/userpresencetype', json={'userId': id}).json()[0]
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^
KeyError: 0

自从我从经验丰富的 roblox api 程序员那里得到它以来,我就期待它能够工作,但它不起作用。

python roblox
1个回答
0
投票

排队:

b = requests.post(f'https://presence.roblox.com/v1/presence/userpresencetype ', json={'userId': id}).json()[0]

您正在尝试访问 JSON 响应的第一个元素,
但 json() 方法返回字典,而不是列表。

您只需访问字典键即可获取必要的数据。

b = requests.post(f'https://presence.roblox.com/v1/presence/userpresencetype', json={'userId': id}).json()

© www.soinside.com 2019 - 2024. All rights reserved.