Twitter APIを使用して特定のユーザーの今日のつぶやきだけを取得する

2024年6月23日

Twitter APIを使用して特定のユーザーの今日のつぶやきだけを取得することができます。以下は、Pythonでtweepyを使用して今日のつぶやきを取得する例です。

まず、tweepyをインストールします。

bash
pip install tweepy

次に、Twitter Developer プログラムに登録し、APIキー、APIシークレット、アクセストークン、アクセストークンシークレットを取得します。

python
import tweepy
from datetime import datetime, timedelta

# Twitter APIの認証情報を設定
consumer_key = 'YourConsumerKey'
consumer_secret = 'YourConsumerSecret'
access_token = 'YourAccessToken'
access_token_secret = 'YourAccessTokenSecret'

# tweepyを使用して認証
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

# 競合のアカウントのツイートを取得
competitor_username = 'CompetitorUsername'

# 今日の日付を取得
today = datetime.today().date()

# ツイートを取得する期間(今日の0時から23時59分59秒まで)
since_date = today.strftime('%Y-%m-%d') + ' 00:00:00'
until_date = today.strftime('%Y-%m-%d') + ' 23:59:59'

# ツイートを取得
tweets = api.user_timeline(screen_name=competitor_username, count=10, since_id=None, max_id=None,
                           tweet_mode='extended', since=since_date, until=until_date)

# 取得したツイートを表示
for tweet in tweets:
    print(tweet.full_text)
    print('----------------------')

このコードでは、sinceおよびuntilパラメータを使用して、指定したユーザーの今日のつぶやきを取得しています。取得したツイートを表示する部分は、実際のアプリケーションに組み込む際に必要に応じて変更できます。

未分類

Posted by ぼっち