Search code examples
pythonapitwittertweepy

Python: Hashtag search with Tweepy


I'd like to get Tweets with #MeTooMen using Tweepy. There are many Tweets using this hashtag as far as I searched Twitter, but I get 0 result when I try to get these Tweets with Tweepy. Do you have any idea what I can do to improve this code?

import os
import tweepy as tw
import pandas as pd

api_key = '*'
api_secret_key = '*'
access_token = '*'
access_token_secret = '*'

auth = tw.OAuthHandler(api_key, api_secret_key)
auth.set_access_token(access_token, access_token_secret)
api = tw.API(auth, wait_on_rate_limit=True)

# Define the search term and the date_since date as variables
search_words = "#metoomen"
date_since = "2017-10-17"
date_until = "2018-01-31"


tweets = tw.Cursor(api.search,
              q = search_words,
              lang = "en",
              since = date_since,
              until = date_until).items(5)

users_locs = [[tweet.user.screen_name, tweet.user.location, tweet.text] for tweet in tweets]

users_locs

>>> []

Solution

  • API.search uses Twitter's standard search API and doesn't accept date_since or date_until parameters:

    Keep in mind that the search index has a 7-day limit. In other words, no tweets will be found for a date older than one week.

    https://developer.twitter.com/en/docs/twitter-api/v1/tweets/search/guides/standard-operators also says:

    [It] is not a complete index of all Tweets, but instead an index of recent Tweets. The index includes between 6-9 days of Tweets.

    You'll need to use the Full-archive premium search API endpoint, with API.search_full_archive, instead.