Python 搜索twitter并从';挖掘社交网络';例子
我正在阅读关于“挖掘社交网络第二版”的代码,我试图了解示例6是如何工作的! 我正试图打印Python 搜索twitter并从';挖掘社交网络';例子,python,twitter,mining,Python,Twitter,Mining,我正在阅读关于“挖掘社交网络第二版”的代码,我试图了解示例6是如何工作的! 我正试图打印状态的长度,并输出不同的结果,下面我将显示两个代码段和每个代码段的结果,我希望有人能向我解释为什么我得到不同的结果。。。提前谢谢 1st code snippet: q = '#python' count = 100 # See https://dev.twitter.com/docs/api/1.1/get/search/tweets search_results = twitter_api.sea
状态的长度,并输出不同的结果,下面我将显示两个代码段和每个代码段的结果,我希望有人能向我解释为什么我得到不同的结果。。。提前谢谢
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个呢 多亏了《挖掘社交网络》一书的作者Matthew A.Russell,他回答了我的问题我想这就是你想要的:
请检查LisaCastellano的解决方案。链接可能会失效,因此不鼓励在so上使用仅链接的答案。请将相关信息复制到您的答案中。