twitter_handles_collection_getting_started
June 22, 2022
1 Twitter API - Getting started
This notebook is to get you started on collecting tweets from the @CommBank Twitter handle.
1.1 Importing packages and defining globals
[14]: import requests
import pandas as pd
import re
from collections import Counter
Before you’re able to collect tweets from the Twitter API, you need to create a developer
account. You can do this by following the steps on this website:
https://developer.twitter.com/en/docs/twitter-api/getting-started/getting-access-to-the-twitter api
Once you’ve done this, you need to create a new App within the Developer Portal
https://developer.twitter.com/en/portal/dashboard. As soon as you’ve done this, your app will
have a unique API_KEY, API_KEY_SECRET and BEARER_TOKEN, which you should insert
into the variables below.
Note: Do not share these tokens!
[7]: API_KEY = "<ADD_API_KEY>"
API_KEY_SECRET = "<ADD_API_KEY_SECRET>"
BEARER_TOKEN = "<ADD_BEARER_TOKEN>"
1.2 Define request parameters
For this task, we are only interested in the tweets associated with the @CommBank handle, so
we define this in the URL parameters. The other variables defined below will be useful when
building your HTTP request to the Twitter API.
You may notice that we are making use of the /search/recent endpoint to collect recent tweets
from an account. We recommend using this endpoint for simplicity.
1
[8]: handle = "CommBank"
url = f"https://api.twitter.com/2/tweets/search/recent?query=from:
↪{handle}&tweet.fields=created_at&expansions=author_id&user.fields=created_at" headers =
{"Authorization": "Bearer {}".format(BEARER_TOKEN)}
1.3 Collect last 100 tweets
Now it is up to you to build the request, send the request and collect the most recent 100 tweets.
Good luck!
[ ]:
[ ]:
[ ]: