Search code examples
pythonregexstringtwitter

Extract tweets that match a string of words exactly


I am trying to get tweets that have an exact match to a string.

Here is the code:

query = "last dance"
language="en"
results = api.search(q=query, lang=language, count=200)

But I get results involving tweets that have the words last and dance separately. But I want tweets that have the words last dance together.


Solution

  • Please, make sure to URL encode these queries before making the request. There are several online tools to help you to do that, or you can search at twitter.com/search and copy the encoded URL from the browser’s address bar. The table below shows some example mappings from search queries to URL encoded queries:

    Search query    URL encoded query
    #haiku #poetry  %23haiku+%23poetry
    “happy hour” :) %22happy%20hour%22%20%3A%29
    

    Note that the space character can be represented by “%20” or “+” sign.

    For more info - https://developer.twitter.com/en/docs/tweets/search/guides/standard-operators