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
Certainly! Here's how you can modify your existing code to print only the last 100 tweets and save them to a CSV file named "kraken_tweets.csv":
import snscrape.modules.twitter as sntwitter import pandas as pd query = "Seattle Kraken" tweets_list = [] for i, tweet in enumerate(sntwitter.TwitterSearchScraper(query).get_items()): if i > 99: break tweets_list.append(vars(tweet)) df = pd.DataFrame(tweets_list) df.to_csv("kraken_tweets.csv", index=False) print(df)In this modified code,
tweets_listis an empty list that will be used to store the tweet dictionaries. We then loop through the first 100 tweets (istarts at 0), adding the dictionary to thetweets_liston each iteration.After the loop, we convert the list of dictionaries to a Pandas DataFrame and save it to a CSV file named "kraken_tweets.csv", with the
index=Falseargument indicating that we do not want to include the row numbers in the CSV file.Finally, we print the DataFrame to the console so we can see the data that was saved to the CSV file.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.