Home > Back-end >  'Cursor' object is not iterable
'Cursor' object is not iterable

Time:01-03

I'm trying to get the location of tweets I've collected using the package tweepy:

import tweepy as tw

consumer = ...
consumer_secret = ...
access_token = ...
access_token_secret = ...

auth = tw.OAuthHandler(consumer, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tw.API(auth, wait_on_rate_limit=True)

search_words = "delante"
date_since = "2022-01-03"

tweets = tw.Cursor(api.search_tweets,
              q = search_words,
              lang = "es",
              since = date_since)

loc = []
for tweet in tweets:
    loc.append(tweet.user.location)

But I get this error:

'Cursor' object is not iterable

The output of type(tweets) is tweepy.cursor.Cursor.

What is it that I'm missing?

CodePudding user response:

The tweepy.Cursor object is indeed not iterable. Call tweets.items() to get an iterator over the individual tweets, or tweets.pages() to get an iterator over pages worth of tweets.

for tweet in tweets.items():
    loc.append(tweet.user.location)

Reference

  • Related