这似乎是世界上最简单的 python 问题......但我将尝试解释一下。
基本上我必须遍历查询的 json 结果页面。
标准结果是这样的
{'result': [{result 1}, {result 2}], 'next_page': '2'}
我需要循环继续循环,将结果键中的列表附加到一个 var 中,以后可以访问并计算列表中的结果数量。但是,我要求它仅在 next_page 存在时循环,因为过了一会儿,当没有更多页面时,next_page 键将从字典中删除。
目前我有这个
next_page = True
while next_page == True:
try:
next_page_result = get_results['next_page'] # this gets the next page
next_url = urllib2.urlopen("http://search.twitter.com/search.json" + next_page_result)# this opens the next page
json_loop = simplejson.load(next_url) # this puts the results into json
new_result = result.append(json_loop['results']) # this grabs the result and "should" put it into the list
except KeyError:
next_page = False
result_count = len(new_result)