2014-01-13 48 views
1

我試圖使用json.loads來獲取所有「查看器」和「時間戳」,並得到它只是爲了輸出它們。在一個網址,但堅持做到這一點。這是代碼:使用json獲取所有「查看器」和「時間戳」

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"} 
+0

你是怎麼卡住的? –

+0

那麼我知道如何獲得第一個查看器,但不是時間戳,我不知道如何獲得其他9個查看器和時間戳。 – user3103923

+0

您是否說過在'url:'中顯示的數據是'data',這是以前比較代碼的值? –

回答

3

它看起來像你可以做這樣的事情:

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

這假定每個辭典該列表具有'查看器'和'時間戳'的鍵。

+0

是的,他們這樣做。我會嘗試。謝謝 – user3103923