Search code examples
pythontweepy

How to get tweets with given position of hashtag


I try to get only Tweets with the hashtag #not, but only when the hashtag is at the end of the Tweet and not in the text. I am using tweepy.Cursor

This code already works. It gives me Tweets with #not, but doen't care where the #not is positioned.

import tweepy
consumer_key = 'consumer key'
consumer_secret = 'consumer secret'
access_token = 'access token'
access_token_secret = 'access token secret'

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth,wait_on_rate_limit=True)

for tweet in tweepy.Cursor(api.search,q="#not",count=5,
                           lang="en",
                           since="2017-04-03").items():
    print (tweet.created_at, tweet.text)

Solution

  • EDIT: You can use a regular expression to check that your hashtag is among a trailing set of hashtags:

    import tweepy
    import re
    
    consumer_key = 'consumer key'
    consumer_secret = 'consumer secret'
    access_token = 'access token'
    access_token_secret = 'access token secret'
    
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = tweepy.API(auth,wait_on_rate_limit=True)
    
    # Regular expression to check if tweet ends with our hashtag and maybe more hashtags
    rgx = re.compile(r"#not(\s+#\w+)*$", re.IGNORECASE)
    for tweet in tweepy.Cursor(api.search,q="#not",count=5,
                               lang="en",
                               since="2017-04-03").items():
        # Keep only tweets with the hashtag at the end
        if rgx.search(tweet.text):
            print (tweet.created_at, tweet.text)
    

    You can just filter the tweets to keep only those matching your requirements:

    import tweepy
    consumer_key = 'consumer key'
    consumer_secret = 'consumer secret'
    access_token = 'access token'
    access_token_secret = 'access token secret'
    
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = tweepy.API(auth,wait_on_rate_limit=True)
    
    for tweet in tweepy.Cursor(api.search,q="#not",count=5,
                               lang="en",
                               since="2017-04-03").items():
        # Keep only tweets with the hashtag at the end
        if tweet.text.lower().endswith('#not'):
            print (tweet.created_at, tweet.text)