import snscrape.modules.twitter as sntwitter query = "Seattle Kraken" for tweet in…
- User
import snscrape.modules.twitter as sntwitter
query = "Seattle Kraken" for tweet in sntwitter.TwitterSearchScraper(query).get_items(): print(vars(tweet))
could you please tell me how to make it only print the last 100 item and also save them to a csv file please and thank you
- Assistant
To get only the last 100 items and save them in a CSV file, you can modify your code like this:
import csv import snscrape.modules.twitter as sntwitter query = "Seattle Kraken" max_tweets = 100 tweets = [] for i, tweet in enumerate(sntwitter.TwitterSearchScraper(query).get_items()): if i >= max_tweets: break tweets.append(vars(tweet)) with open("tweets.csv", "w", newline="", encoding="utf-8") as csvfile: fieldnames = tweets[0].keys() writer = csv.DictWriter(csvfile, fieldnames) writer.writeheader() writer.writerows(tweets)This code first initializes an empty list called
tweets. After collecting a tweet, it appends the tweet to thetweetslist. The loop breaks if the number of collected tweets reaches your maximum limit (100).The final part uses Python's built-in CSV library to save the list of dictionaries to a CSV file called
tweets.csv. This files contains the collected tweet data with the same column order as in the first dictionary.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.