Search code examples
python-3.xtwittertweepy

How to search for historical data from Twitter with Tweepy


I'm new to Python and need some help. I'm trying to search for historical tweets from 2014 to 2018 from specific Twitter users with specific keywords in a research project and have a few questions for my code.

  1. I can't find a code on how to get the output as a number of tweets instead of a long list of many tweets. Is this possible?
  2. Does anyone know how to connect the endpoint from Twitter Developer to the code? I will use a Premium API account.
  3. I'm getting an Error on the code because of the functions: from and filter, is there a way to fix this? I tried to insert it in the same " " as the query, but that didn't work.
#Endpoint from Twitter Developer 
endpoint = 'https://api.twitter.com/1.1/tweets/search/30day/dev.json'

#All keywords
query = 'xxxx' or 'yyyy'

#Search code
search = tweepy.Cursor(api.search, q = query, "from:TwitterUser -filter:retweets", lang = 'en').items()
for item in search:
    print (item.text)

And as an additional question not related to my code, is it possible to search for retweets and likes by specific users on certain tweets? Lets say I'm searching for tweets from user1, can I then see how many times user2, user3 and user4 have retweeted and liked user1's tweets?

Thanks for any help!

Cheers


Solution

  • I don't believe that tweepy currently provides support for the premium search endpoints (additionally, I see that your code is hitting a 30 day endpoint, when for your use case you would need to access the full archive search endpoint).

    For premium search I would recommend that you look at either TwitterAPI, or the search-tweets-python library that we (Twitter) provide. In the case of the latter, it also supports the counts endpoint, but be aware that these are only available in the paid tier, not the free sandbox. Also, note that the search syntax on the premium search API is different to standard search. Check the documentation.

    On your last question unrelated to code - no, there's no API that would enable you to find that information.