search twitter and collect search results from 'mining the social web' examples

2.3k views Asked by At

I'm reading the code for 'mining the social web 2nd E' on here and I'm trying to understand how example 6 is working! I'm trying to print the length of statuses and is outputting different results, below I will display two code snippets and the results for each one and I hope if somebody can explain to me why I'm getting different results... thanks in advance.

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

the output is:

Length of statuses 100
Length of statuses 100
Length of statuses 100
Length of statuses 100
Length of statuses 100

which is exactly what I'm expecting. but if I add this to the above code:

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']

the output will be:

Length of statuses 100
Length of statuses 200
Length of statuses 200

my question is why in the second time it prints only three batches and not five as the for loop is set to loop five times?? and why they are not of 100 count each?

2

There are 2 answers

0
tkyass On BEST ANSWER

Thanks to Matthew A. Russell the author of 'Mining the Social Web', he answered my question HERE

1
nietzschetmh On

I think this is what you are looking for:

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

Please check the solution from LisaCastellano.