我正在阅读此处“挖掘社交网络 2nd E”的代码,并试图了解示例 6 的工作原理!我正在尝试打印的长度statuses
并正在输出不同的结果,下面我将显示两个代码片段和每个代码片段的结果,我希望有人能向我解释为什么我会得到不同的结果......提前谢谢.
1st code snippet:
q = '#python'
count = 100
# See https://dev.twitter.com/docs/api/1.1/get/search/tweets
search_results = twitter_api.search.tweets(q=q,count=count)
statuses = search_results['statuses']
# Iterate through 5 more batches of results by following the cursor
for _ in range(5):
print "Length of statuses", len(statuses)
try:
next_results = search_results['search_metadata']['next_results']
except KeyError, e: # No more results when next_results doesn't exist
break
输出是:
Length of statuses 100
Length of statuses 100
Length of statuses 100
Length of statuses 100
Length of statuses 100
这正是我所期待的。但是如果我将它添加到上面的代码中:
q = '#python'
count = 100
# See https://dev.twitter.com/docs/api/1.1/get/search/tweets
search_results = twitter_api.search.tweets(q=q,count=count)
statuses = search_results['statuses']
# Iterate through 5 more batches of results by following the cursor
for _ in range(5):
print "Length of statuses", len(statuses)
try:
next_results = search_results['search_metadata']['next_results']
except KeyError, e: # No more results when next_results doesn't exist
break
# Create a dictionary from next_results, which has the following form:
# ?max_id=313519052523986943&q=NCAA&include_entities=1
kwargs = dict([ kv.split('=') for kv in next_results[1:].split("&") ])
search_results = twitter_api.search.tweets(**kwargs)
statuses += search_results['statuses']
输出将是:
Length of statuses 100
Length of statuses 200
Length of statuses 200
我的问题是为什么在第二次它只打印三个批次而不是五个,因为 for 循环设置为循环五次?以及为什么它们每个都不是 100 计数?