Extract Tweet using Tweepy with specific fields
I am trying to extract tweets based on certain hashtags or keywords and I want to get the following information: date, username (who tweeted), number of retweets and number of likes.
I want all this information in column row format and export it in Excel or CSV format. I tried the below code but I can't get everything.
How can i do this?
import tweepy
import csv
ckey = "*************"
csecret = "******************"
atoken = "****************"
asecret = "************************"
OAUTH_KEYS = {'consumer_key':ckey,'consumer_secret':csecret,'access_token_key':atoken,'access_token_secret':asecret}
auth=tweepy.OAuthHandler(OAUTH_KEYS['consumer_key'],OAUTH_KEYS['consumer_secret'api = tweepy.API(auth)
testTweet = tweepy.Cursor(api.search, q="Trump AND H1B").items(1000)
for tweet in testTweet:
print (tweet.created_at, tweet.text, tweet.lang, tweet.user)
+3
source to share
1 answer
Tweepy uses the Twitter API, so you can retrieve all tweet information using these fields .
You can change your code like this:
for tweet in testTweet:
text = tweet.text
language = tweet.lang
date = tweet.created_at
username = tweet.user
retweets = tweet.retweet_count
likes = tweet.favorite_count
In the end, if you want to store all the information in an Excel file, I suggest you use the xlsxwriter . This package handles .xls and .xlsx formats and is easy to use.
Otherwise, if you want to use CSV check out this Stack Overflow question .
+2
source to share