搜索Twitter并从“挖掘社交网络”示例中收集搜索结果

时间:2013-08-30 20:12:33

标签: python twitter mining

我正在阅读here上的'挖掘社交网络第二个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?

2 个答案:

答案 0 :(得分:0)

感谢“挖掘社交网络”一书的作者Matthew A. Russell,他回答了我的问题HERE

答案 1 :(得分:0)

我认为这就是你要找的东西:

https://github.com/ptwobrussell/Mining-the-Social-Web-2nd-Edition/issues/212

请检查LisaCastellano的解决方案。

相关问题