好的,我正在尝试在跟踪器中获取人员和时间戳,但我一直收到此错误:
list indices must be integer, not str
这是获取该错误的代码:
elif used_prefix and cmd == "tracker" and self.getAccess(user) >= 1:
try:
if len(args) == 0:
name = user.name.lower()
elif len(args) > 0:
name = args.lower()
f = urlreq.urlopen("http://chtracker.info/index.py?output=json&x=page&y={}".format(name))
data = json.loads(f.readall().decode("utf-8"))['viewer'][:10]
t = "{}, the last 10 people who visited {}'s profile are: ".format(name.title(), name.title())
viewers = [(item['viewer'], item['timestamp']) for item in data]
for l in viewers:
viewer, timest = l
t += "{} about {} ago".format(viewer, stamp.getTimeStamp((timest)))
room.message(t, True)
except:
print(traceback.format_exc())
room.message((str(sys.exc_info()[1])))
错误发生在这一行:
data = json.loads(f.readall().decode('utf-8'))['viewer'][:10]
在网址中,dictionarys具有相同的标签“viewer”和“timestamp”,例如:
http://chtracker.info/index.py?output=json&x=page&y=mechabot
答案 0 :(得分:2)
json.loads(...)
正在返回list
,所以当你这样做时:
json.loads(...)['viewer']
您正尝试访问索引为list
且无效的'viewer'
。
您可以通过打印json.loads(f.readall().decode("utf-8"))
来实现这一点,这样您就可以找到正确的方法来访问它的价值。