Categories
APIs General Python

Search Twitter from Your Command Line with Python

Recently I was curious to see Twitter’s sentiment on Black Friday. It turns out, so was much of the internet, as I got hundreds of views on my post within hours. In this post, we’re going to walk through how you can search Twitter from your command line with Python. To follow this tutorial, you’ll need to sign up for Twitter’s Developer Platform and install the requests module. Go to the link to sign up for Twitter’s API and use the following line in your terminal to install the requests module:

pip install requests

Setting Up Constants For the Search Request

As always, we will begin our program with our imports. We’ll need to import the requests library to send HTTP requests and the json library to parse the response. I’ve also imported the Bearer Token that we get from signing up for the Twitter API here. I saved my Bearer Token in a config file, but you are welcome to access it however you’d like. 

After the imports we set up the constants. First, we’ll define the endpoint. This is the endpoint to search the last seven days for Tweets. We also need to define the headers we’ll pass. To use the API, all we need to pass in the header is an Authorization key telling the server what our Bearer Token is.

import requests
import json
from config import bearertoken
 
search_recent_endpoint = "https://api.twitter.com/2/tweets/search/recent"
headers = {
    "Authorization": f"Bearer {bearertoken}"
}

Creating the Search Function

Once we have the constants set up, let’s create our search function. This function will take one parameter – the search term in the form of a string. We’ll use the search term to build the query. The easiest way to do this is to use an f string and simply pass in the search term with brackets. You can also add different parameters to your query to narrow your search. In this example, our query is looking for English Tweets with no links and are not retweets.

We’ll use the query in a params dictionary that we pass to our search endpoint. We can only pass params and not json or data in our request because it is a GET request. Once we get the response back, we’ll parse the text with the json library. Finally, we’ll dump the parsed request into a JSON file for further processing.

# automatically builds a search query from the requested term
# looks for english tweets with no links that are not retweets
# saves the latest 100 tweets into a json
def search(term: str):
    params = {
        "query": f'{term} lang:en -has:links -is:retweet',
        'max_results': 100
    }
    response = requests.get(url=search_recent_endpoint, headers=headers, params=params)
    res = json.loads(response.text)
    with open(f"{term}.json", "w") as f:
        json.dump(res, f)

Searching Twitter from the Command Line

Now that we’ve created the search function, we’re ready to use it. All we need is a search term. In this example, we’ll prompt the user for a search term they’d like to search Twitter for. Then, we simply call the search function.

term = input("What would you like to search Twitter for? ")
search(term)

The images below show what this program looks like in action.

After running, this will create a file in our folder called starbucks.json. When we look through that file it will look like the image below.

That’s all you need to do to use Python to search Twitter from your command line!

I run this site to help you and others like you find cool projects and practice software skills. If this is helpful for you and you enjoy your ad free site, please help fund this site by donating below! If you can’t donate right now, please think of us next time.

Leave a ReplyCancel reply