Fakhr Ali
Fakhr Ali

Reputation: 128

How to get the likes of every tweet containing a specific hashtag with tweepy

I can retrieve tweets with a specific hashtag using tweepy:

Code:

from os import access
import tweepy 
import configparser
import pandas as pd

# config = configparser.ConfigParser()
# config.read('config.ini')

api_key = ''
api_key_secret = ''

access_token = ''
access_token_secret = ''

auth = tweepy.OAuthHandler(api_key, api_key_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

# user = '@veritasium'
keywords = '#SheHulk'
limit = 1200

tweets = tweepy.Cursor(api.search_tweets, q = keywords, count = 100, tweet_mode =     'extended').items(limit)


columns = ['User', 'Tweet']
data = []

for tweet in tweets:
    data.append([tweet.user.screen_name, tweet.full_text])

df = pd.DataFrame(data, columns=columns)
df.to_excel("output.xlsx")

What I want to know is that if I can get the number of likes with every tweet that is retrieved. Any help would be appreciated.

Upvotes: 0

Views: 318

Answers (1)

Mickaël Martinez
Mickaël Martinez

Reputation: 1843

In the Twitter API V1.1 (see documentation here), that field was called favorite_count.

for tweet in tweets:
    print(f"That tweet has {tweet.favorite_count} likes").

Upvotes: 1

Related Questions