Get tweets containing keywords using Python Tweepy

I want to get tweets that include the #Weekend Hackathon keyword when creating a weekend hackathon website. Get it using Python's tweepy module.

Get Consumer API keys and Access token & access token secret

Register the application from Twitter Developers and get the Consumer API keys and Access token & access token secret.

tweepy installation

Tweepy is convenient for operating the Twitter API, so please use it.

$ pip install tweepy

OAuth authentication

OAuth authentication required to operate Twitter API Authenticate using the API KEY obtained from the Developer site.

import tweepy


#Enter the obtained API KEY and TOKEN
API_KEY = ""
API_SECRET_KEY = ""
ACCESS_TOKEN = ""
ACCESS_TOKEN_SECRET = ""


def twitter_api() -> tweepy.API:
    auth = tweepy.OAuthHandler(API_KEY, API_SECRET_KEY)
    auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
    return tweepy.API(auth)

Keyword search

Created get_search method to enable keyword search

** Argument description **

KEY Contents
api OAuth authentication
q Keywords you want to search
start_date Search start period
end_date Search end period
count Number of acquisitions

Since the data is easy to process, I decided to put it in Pandas tweet_created_at is changed to Japan time after +9 hours because it is US time

from datetime import timedelta

import pandas as pd
import tweepy

#Enter the obtained API KEY and TOKEN
API_KEY = ""
API_SECRET_KEY = ""
ACCESS_TOKEN = ""
ACCESS_TOKEN_SECRET = ""


def twitter_api() -> tweepy.API:
    auth = tweepy.OAuthHandler(API_KEY, API_SECRET_KEY)
    auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
    return tweepy.API(auth)


def get_search(
    api: tweepy.API, q: str, start_date: str, end_date: str, count: int = 1000
) -> pd.DataFrame:

    q = f"{q} since:{start_date} until:{end_date} -filter:retweets"

    tweets = api.search(
        q=q,
        count=count,
        tweet_mode="extended",
        locale="ja",
        lang="ja",
        include_entities=False,
    )

    df = pd.DataFrame(
        columns=[
            "user_id",
            "user_name",
            "user_screen_name",
            "user_profile_image_url",
            "tweet_id",
            "tweet_full_text",
            "tweet_favorite_count",
            "tweet_created_at",
        ]
    )

    for tweet in tweets:
        df = df.append(
            {
                "user_id": tweet.user.id,
                "user_name": tweet.user.name,
                "user_screen_name": tweet.user.screen_name,
                "user_profile_image_url": tweet.user.profile_image_url.replace(
                    "_normal", ""
                ),
                "tweet_id": tweet.id,
                "tweet_full_text": tweet.full_text,
                "tweet_favorite_count": tweet.favorite_count,
                "tweet_created_at": tweet.created_at + timedelta(hours=+9),
            },
            ignore_index=True,
        )
    return df

How to use

Simple OAuth authentication and search, DataFrame so process it and use it

api = twitter_api()
search = get_search(api, "#Weekend hackathon", "2021-01-15", "2021-01-18")

idxmax = search.groupby("user_id").tweet_created_at.idxmax()
tweets = search.iloc[idxmax]

Recommended Posts

Get tweets containing keywords using Python Tweepy
Exclude tweets containing URLs with tweepy [Python]
Get Tweets with Tweepy
Try using Tweepy [Python2.7]
Get tweets with arbitrary keywords using Twitter's Streaming API
Continue to retrieve tweets containing specific keywords using the Streaming API in Python
Collect tweets using tweepy in Python and save them in MongoDB
Get data from Twitter using Tweepy
Python> dictionary> values ()> Get All Values by Using values ()
Get Suica balance in Python (using libpafe)
Get replies to specific tweets with tweepy
Get Youtube data in Python using Youtube Data API
Get lots of your tweets with Tweepy
[Python] Get all comments using Youtube Data API
Get image URL using Flickr API in Python
Start using Python
Get note information using Evernote SDK for Python 3
Scraping using Python
Get LEAD data using Marketo's REST API in Python
[Python] Get insight data using Google My Business API
Save tweets containing specific keywords in CSV on Twitter
Get and automate ASP Datepicker control using Python and Selenium
Get files from Linux using paramiko and scp [Python]
Fibonacci sequence using Python
Collecting tweets with Python
Data analysis using Python 0
[Python] Get Qiita trends
Posting tweets with python
Data cleaning using Python
Using Python #external packages
WiringPi-SPI communication using Python
Age calculation using python
[Python3] Get date diff
Get date in Python
Get date with python
Search Twitter using Python
Name identification using python
Notes using Python subprocesses
python get current time
A Python program that collects tweets containing specific keywords daily and saves them in csv
Get an English translation using python google translate selenium (memories)
Get Python scripts to run quickly in Cloud Run using responder