4

Bear with me, since I am still learning Python..

I want to be able to see the most trending tweets for a specific hashtag. I am able to find trending tweets and I am able to find tweets with a specific hashtag but I am at a loss when trying to combine these two.

import tweepy

CONSUMER_KEY = 'key'
CONSUMER_SECRET = 'secret'
OAUTH_TOKEN = 'key'
OAUTH_TOKEN_SECRET = 'secret'

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

api = tweepy.API(auth)
trends = api.trends_place(1)

search_hashtag = tweepy.Cursor(api.search, q='hashtag').items(5000)

for tweet in search_hashtag:
    print json.dumps(tweet)

print json.dumps(trends, indent=1)

This is what i have now that is working..

Thanks!

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Roronoa
  • 55
  • 2
  • 8

2 Answers2

1

From: https://dev.twitter.com/rest/public/search

It allows queries against the indices of recent or popular Tweets and behaves similarly to, but not exactly like the Search feature available in Twitter mobile or web clients, such as Twitter.com search. The Twitter Search API searches against a sampling of recent Tweets published in the past 7 days.

Therefore, you are pretty much already getting what you want. These are actually trending tweets for specific hashtag/topic.

https://dev.twitter.com/rest/reference/get/trends/place is simply meant to get trending topics for specific location, but from your question it seems you want to search by some specific topic, not the one that is currently tr

vith
  • 8,916
  • 1
  • 12
  • 4
  • 2
    is it possible to get tending tweets in between some dates? Like there are only 2 params for `trends/place.json`. What can be done in this case? – vam Apr 12 '18 at 07:32
0

If I understand correctly when you say "I want to be able to see the most trending tweets for a specific hashtag." you mean that you want the tweets that have certain hashtags that are the most trendy (popular).

Well, thinking about it I came across with an idea. You could use the hastag as a query, and then search among the tweets collected which ones have the higest number of retweets.

The code may look something like this:

import tweepy

CONSUMER_KEY = 'key'
CONSUMER_SECRET = 'secret'
OAUTH_TOKEN = 'key'
OAUTH_TOKEN_SECRET = 'secret'

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

api = tweepy.API(auth)
trends = api.trends_place(1)

search_hashtag = tweepy.Cursor(api.search, q='hashtag', tweet_mode = "extended").items(5000)

for tweet in search_hashtag:
        if 'retweeted_status' in tweet._json: 
            full_text = tweet._json['retweeted_status']['full_text']
        else:
            full_text = tweet.full_text
                        
        tweets_list.append([tweet.user.screen_name,
                            tweet.id,
                            tweet.retweet_count, # What you are interested of
                            tweet.favorite_count, # Maybe it is helpfull too
                            full_text,
                            tweet.created_at,
                            tweet.entities
                           ])
tweets_df = pd.DataFrame(tweets_list, columns = ["screen_name", "tweet_id",
                                                  "nº rt", 
                                                  "nº replies",
                                                  "text",
                                                  "created_at", 
                                                  "entities"])
# In a data frame format you can sort the tweets by the nº of rt and get the top one
tweets_df.to_json()

As you can see I have used tweet_mode = "extended" becuase probably you are interested in the whole text (by default Twitter API trunctate the text). Moreover, you would like to use the number of replies combined with the number of retweet of a tweet to get the most trendy. Hope you find it useful! Happy coding!!!

Álvaro H.G
  • 397
  • 1
  • 10