snscrape icon indicating copy to clipboard operation
snscrape copied to clipboard

Specify the number of tweets to the snscrape.modules.twitter moduler

Open aoamusat opened this issue 3 years ago • 2 comments

from snscrape.modules import twitter

tweet_list = list(twitter.TwitterSearchScraper("Spacex").get_items())

The get_items() should be passed an argument n to specify the number of tweets to get from the TwitterSearchScraper

aoamusat avatar Nov 14 '22 09:11 aoamusat

That's meant for you to do. For example:

tweet_generator = twitter.TwitterSearchScraper("Spacex").get_items()
tweet_list = []
max = 100
for count, tweet in tweet_generator.enumerate():
    if count >= max:
         break # reached max
    tweet_list.append(tweet)

List comprehension would probably work too but I'm at school.

TheTechRobo avatar Nov 14 '22 14:11 TheTechRobo

itertools.islice would be the most elegant approach. But yeah, this is best solved outside of snscrape since there are so many different ways how someone might want to filter results. First n results is probably a somewhat common one, but an itertools.islice example in the future documentation would be a better solution for that.

JustAnotherArchivist avatar Nov 14 '22 21:11 JustAnotherArchivist