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 the code to print only the last 100 items and save them to a CSV file:
import snscrape.modules.twitter as sntwitter import pandas as pd
query = "Seattle Kraken"
create an empty list to store the tweets
tweets = []
use a counter to limit the number of tweets to 100
count = 0
for tweet in sntwitter.TwitterSearchScraper(query).get_items(): if count < 100: tweets.append(vars(tweet)) count += 1 else: break
convert the list of tweets to a Pandas DataFrame
df = pd.DataFrame(tweets)
save the DataFrame to a CSV file named "tweets.csv"
df.to_csv("tweets.csv", index=False)
print the first few rows of the DataFrame
print(df.head())
This code creates an empty list called "tweets" to store the tweets. Then it uses a counter called "count" to limit the number of tweets to 100. Each time a tweet is iterated over, the code checks whether the counter is less than 100. If it is, the tweet is added to the "tweets" list and the counter is incremented. If it is not, the loop is exited using the "break" keyword.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.