循环使用Web服务结果无效

时间:2016-05-07 18:03:46

标签: python list for-loop

我正在尝试使用Web服务并循环遍历结果集。 出于某种原因,它只给了我第一个结果,在这种情况下是:

  

总排名:537316等级:1419 Xp:6407333

似乎停止并且不在stat_list函数的grab_api列表中列出其余结果。

stat_list是列表中的列表,其中包含数字字符串。

import urllib2
import re

def grab_api():
stat_list = []
response = urllib2.urlopen('http://services.runescape.com/m=hiscore/index_lite.ws?player=taroboxx')
html = response.read()
stat_list.append(re.split(r"[,\n]", html))
return stat_list

def check_score(hiscore_html):
stats = ["Overall", "Attack", "Defence", "Strength", "Constitution", "Ranged", "Prayer", "Magic", "Cooking", "Woodcutting", "Fletching", "Fishing", "Firemaking", "Crafting", "Smithing", "Mining", "Herblore", "Agility", "Thieving", "Slayer", "Farming", "Runecrafting", "Hunter", "Construction", "Summoning", "Dungeoneering", "Divination"]
hiscore = 0
stat = 0
for i in hiscore_html[0]:
    if hiscore == 0:
        print stats[stat],
        print "Rank:", i,
        stat += 1
        hiscore += 1
    elif hiscore == 1:
        print "Level:", i,
        hiscore += 1
    elif hiscore == 2:
        print "Xp:", i
        hiscore += 1
    else:
        hiscore == 0

check_score(grab_api())

2 个答案:

答案 0 :(得分:0)

else区块中,您正在hiscore == 0而不是hiscore = 0

答案 1 :(得分:0)

作为第一个函数的输出,你得到一个列表列表,所以你想要的列表中没有迭代:你去一个列表(hiscore_html),在里面你找到另一个列表,这是唯一的项目hiscore_html。

为了避免它,你可以参考hiscore_html的第一个元素。但我刚看到你的更新 - 它正是你正在做的事情,另一个错误在下面修复。