我正在尝试使用json.loads来获取所有的“查看器”和“时间戳”,并使其仅用于输出它们。在网址中,但在执行此操作时遇到问题。这是代码:

elif used_prefix and cmd == "tracker" and self.getAccess(user) >= 1:
   try:
      if len(args) == 0:
        f = urlreq.urlopen("http://chtracker.info/index.py?output=html&x=page&y={}&results=0".format(user.name))
        data = json.loads(f.readall().decode("utf-8"))
        viewers = [(item['viewer'], item2['timestamp']) for item, item2 in data]
        room.message("The last 10 people who visited your profile: %s %s ago".format(item, stamp.getTimeStamp(item2)))
   except:
      room.message((str(sys.exc_info()[1])))


网址中的内容如下:

[{"viewer": "terraria11218", "timestamp": "1389575253.25377", "referer": "http://www.animefave.net/my-little-sister-cant-be-this-cute-season-2-episode-1/", "ip": "108.14.254.110", "useragent": "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36", "page": "mechabot"}, {"viewer": "oruni", "timestamp": "1389574404.94542", "referer": "http://www.pinoyanime.tv/btooom-episode-3/", "ip": "75.187.235.72", "useragent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.72 Safari/537.36", "page": "mechabot"}

最佳答案

看来您可以执行以下操作:

>>> viewers = [(item['viewer'], item['timestamp']) for item in data]
[('terraria11218', '1389575253.25377'), ('oruni', '1389574404.94542')]
>>> viewers
[('terraria11218', '1389575253.25377'), ('oruni', '1389574404.94542')]


假定列表中的每个字典都有键“ viewer”和“ timestamp”。

关于python - 使用json获取所有“查看器”和“时间戳”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21082709/

10-13 02:08