Search code examples
pythontwittertweepytwitter-api-v2

Twitter api not returning 'created_at' info for tweets


When running the following code using the Python Tweepy library:

query = '$MSFT'
start_time = '2019-01-01T00:00:00Z'
end_time = '2019-02-01T00:00:00Z'
max_results = 10

results = client.search_all_tweets(query=query, max_results=max_results, start_time=start_time, end_time=end_time)

I receive a bunch of tweets. However, these tweets only contain the following data:

tweet.edit_history_tweet_ids
tweet.id
tweet.text

Information like 'created_at' or 'geo' are missing. Anyone have any idea what is going wrong here?


Solution

  • Adding tweet_fields parameter with specific field can see created_at but geo usually missed in tweet due to not added by tweeter.

    It can't see most case even if passing geo

    I can't test it due to no paid account but I can test recent 7 days result. as attached code and results. The search_recent_tweets() and search_all_tweets() almost same parameters.

    Here is documentation.

    enter image description here

    It is matched V2 low level API in here

    GET /2/tweets/search/all
    

    enter image description here

    import tweepy
    
    bearer_token ="***** your bearer_token *****"
    
    query = '\$MSFT'
    start_time = '2019-01-01T00:00:00Z'
    end_time = '2019-02-01T00:00:00Z'
    max_results = 10
    
    results = client.search_all_tweets(query=query, 
        tweet_fields=['created_at','geo'],
        max_results=max_results,
        start_time=start_time,
        end_time=end_time)
    
    for result in results.data:
        print(result.data)
    
    import tweepy
    
    bearer_token ="***** your bearer_token *****"
    
    client = tweepy.Client(bearer_token=bearer_token)
    
    query="\$MSFT"
    results = client.search_recent_tweets(query=query, 
                tweet_fields=['created_at','geo'],
                start_time='2023-01-12T00:03:00Z',
                max_results=10
                )
    
    for result in results.data:
        print(result.data)
    

    Result

    >python search.py
    {'created_at': '2023-01-16T01:27:49.000Z', 'text': '@excel_ranger $MSFT', 'edit_history_tweet_ids': ['1614796530795298817'], 'id': '1614796530795298817'}
    {'created_at': '2023-01-16T01:27:26.000Z', 'text': 'Live day trading, detailed analysis on stocks with entry, exit, stop losses, price targets,mentoring and live news\n\nhttps://xxx/LzwYLosqbb\n\n$BIDU $SAVE $WORK $T $AAPL $C $MSFT $SPY $FB $CHWY $PTON $DIS $F $ADBE $CSCO $MGM $XOM $AMD $ZI $Z $BA $DOW $NET $PROP $OSTK  $QCOM $ADM https://xxx/n3T6xDyNNj', 'edit_history_tweet_ids': ['1614796435139993607'], 'id': '1614796435139993607'}
    {'created_at': '2023-01-16T01:27:10.000Z', 'text': '$SPY $QQQ $DIS $TSLA $SHOP\n$AMD $AAPL $SQ $AMZN \n$EA $SEDG $MA $V $KO $PYPL $RCL $GOOG $NKLA $DKNG $HD $ROKU $NFLX $FB  $GILD $VXX $MSFT\n\nThanks to the discord group for the traders šŸ™https://xxx/LzwYLosqbb https://xxx/V3pz7tESOk', 'edit_history_tweet_ids': ['1614796367511035904'], 'id': '1614796367511035904'}
    {'created_at': '2023-01-16T01:26:56.000Z', 'text': 'RT @DarthDividend23: Great post by @thedividendclub on IG! \n\n$WMT $APD $JNJ $PG $WBA $GILD $O $MRK $UPS $AAPL $COST $SBUX $TGT $LOW $MSFT $ā€¦', 'edit_history_tweet_ids': ['1614796311076769794'], 'id': '1614796311076769794'}
    {'created_at': '2023-01-16T01:26:52.000Z', 'text': 'RT @TrendSpider: Do the Markets need a Breather? šŸ§\n\nPrepare for the week ahead šŸ“\n\nTickers Covered:\n$SPY 00:00\n$QQQ 01:58\n$IWM 04:57\n$BTC 07ā€¦', 'edit_history_tweet_ids': ['1614796291812327428'], 'id': '1614796291812327428'}
    {'created_at': '2023-01-16T01:26:46.000Z', 'text': 'šŸš€Alerted an entry for $METX win of 121%. \nhttps://xxx/LzwYLosqbb\nāœ…Check us out\n\nšŸ”„\n\n$NAKD $TSLA $AAPL $NIO $SPY $INTC $GE $SNDL $NXTD $KGC $ZOM\n$FCEL\n$FEYE \n$ZM\n$AAL \n$PLTR \n$CCL \n$MSFT \n$PFE\n$PLUG \n$WFC \n$AMD \n$VXX \n$CNSP $QQQ https://xxx/OZFbKDpaHp', 'edit_history_tweet_ids': ['1614796270048010243'], 'id': '1614796270048010243'}
    {'created_at': '2023-01-16T01:25:50.000Z', 'text': "STOCK, OPTIONS updates, alerts Free chatroom\nDon't forget to take a trial! \nhttps://xxx/LzwYLosqbb\n\n$SPY $BABA $DVAX $ACB $OSTK $TRIL $LK $CODX $SAVE $GSX $INO $KSS $PENN $NVAX $NIO $AAL $NKLA $MSFT $AAPL $AMZN $TSLA $CCL $BILI $CVNA $DAL $TWTR https://xxx/cT6Lc9Yhpv", 'edit_history_tweet_ids': ['1614796034487603200'], 'id': '1614796034487603200'}
    {'created_at': '2023-01-16T01:25:36.000Z', 'text': '$MSFT at $220 feels like stealing candy from a baby.', 'edit_history_tweet_ids': ['1614795972751785985'], 'id': '1614795972751785985'}
    {'created_at': '2023-01-16T01:25:16.000Z', 'text': '$DXY Index looks to test 101.60 as odds of less-hawkish Fed bets soar https://xxx/9PYMyxzvpt  $CRM $DXY $NIO $BABA $BTC.X $ETH.X $BLK $COIN $BNB.X $AAPL $TSLA $MULN $CEI $SPY $DJIA $QQQ $WMT $MSFT $PFE $MRNA $AZN $ABNB $AMD $BNTX $BA $COP $PDD $COST $GM $META $AMZN $NFLX', 'edit_history_tweet_ids': ['1614795889251422208'], 'id': '1614795889251422208'}
    {'created_at': '2023-01-16T01:24:52.000Z', 'text': '$AAPL Trade ideašŸ’”\nhttps://xxx/LzwYLosqbb\n\n$AMC $SPY $GME $QQQ $MU $MSFT $AMD $PTON $AMZN $CRM $XLF $XLE $TSLA $AAL https://xxx/rDouPd3hNj', 'edit_history_tweet_ids': ['1614795790412648450'], 'id': '1614795790412648450'}