import snscrape.modules.twitter as sntwitter import pandas as pd query = "Seattle…
- User
import snscrape.modules.twitter as sntwitter import pandas as pd
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 print only the last 100 items and save them to a CSV file, you can use the following edited code:
import snscrape.modules.twitter as sntwitter import pandas as pd query = "Seattle Kraken" max_results = 100 # Scrape tweets and store them in a list tweets = [] for i, tweet in enumerate(sntwitter.TwitterSearchScraper(query).get_items()): tweets.append(vars(tweet)) if i + 1 >= max_results: break # Store the last 100 tweets. If there are less than 100 tweets, store all. if len(tweets) >= 100: last_100_tweets = tweets[-100:] else: last_100_tweets = tweets # Print the last 100 tweets for tweet in last_100_tweets: print(tweet) # Save the last 100 tweets to a CSV file df = pd.DataFrame(last_100_tweets) df.to_csv("last_100_tweets.csv", index=False)This code scrapes tweets with the given query, storing them in a list, and limits the number of items to 100 using an "if" condition. It then slices the list to get the last 100 items (or all of them if there are less than 100 tweets). The code then goes on to print the last 100 tweets and saves them to a CSV file using Pandas.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.