Search code examples
pythonapiweb-scrapingtwittertweepy

How to get all tweets containing a certain username or name


I am writing a code using the tweepy library to collect all tweets containing a specific user id. For this example let's say I want to find all tweets related to Austrian Airlines

What I would do to achieve such goal (assuming I have access to the twitter API) is something like this:

import pandas as pd
import numpy as np
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
from tweepy import API
from tweepy import Cursor

auth = OAuthHandler(twitter_credentials['CONSUMER_KEY'], twitter_credentials['CONSUMER_SECRET'])
auth.set_access_token(twitter_credentials['ACCESS_TOKEN'], twitter_credentials['ACCESS_TOKEN_SECRET'])
api = API(auth,wait_on_rate_limit=True,wait_on_rate_limit_notify=True)

# Search word/hashtag value 
HashValue = '_austrian'
# search start date value. the search will start from this date to the current date.
StartDate = "2019-11-11" # yyyy-mm-dd

for tweet in Cursor(api.search,q=HashValue,count=1,lang="en",since=StartDate, tweet_mode='extended').items():
    print (tweet.created_at, tweet.full_text)

However this approach doesnt seem to return what I expect. I just get a series of tweets where the word austrian is mentioned.

What should i do in order to get just the tweets containing _austrian?


Solution

  • What I would do is use this package instead: GetOldTweets3

    I used the following code.

    tweetCriteria = got.manager.TweetCriteria().setQuerySearch('@_austrian')\
                                           .setSince("2019-11-11")\
                                           .setMaxTweets(10)
    tweet = got.manager.TweetManager.getTweets(tweetCriteria)
    

    Currently, its set to look for all tweets that contains '_austrian' with a given date and is limited to 10 tweet searches on the code. Adjust according to your needs.

    To loop through the results you'll need to for loop it.

    for item in tweet:
      print(item.username, item.text)
    

    Sample Output

    HofmannAviation In the 1980s I joined a #tyrolean Airways Dash 7 pilot training flight to Courchevel in the French Alps. In the Cockpit also Armin Kogler @_austrian @AHoensbroech @Flugthier @AlexInAir @_ABierwirth_ #dash7 @courchevel @BBD_Aircraft @GabyAttersee @AgueraMartin @GuillaumeFaurypic.twitter.com/NULpX4WSkA
    

    You can read more on the github page on how you can control the searches. You can get more than the usernames and the content using this package.