クリエイターの教科書

ChatGPT, Stable Diffusion, Visual Studio, Excelなど教科書

【Python】Tweetの取得「Tweepy」

PythonでTweetの取得をするTweepyの紹介。

Tweepyとは?

Bluebird

Twitterのデータを取得するライブラリです。

 

TwitterのAPIを利用します。

 

TwitterのAPIについては規約や料金などがめまぐるしく変わっている印象です。

 

 

Tweepyの使い方

先にTwitterへデベロッパ登録し、アプリを申請。

Twitter Developers

 

コンシューマキーやTokenを取得しておきます。

 

 

プログラムでは、コンシューマキー、コンシューマシークレット、アクセストーケン、トーケンのシークレットを指定。

import tweepy

api_key = 'api_key'
api_key_secret = 'api_key_secret'
access_token = 'access_token'
access_token_secret = 'api_token_secret'

auth = tweepy.OAuth1UserHandler(
    api_key, api_key_secret, access_token, access_token_secret
)
api = tweepy.API(auth)

 

 

 あとは指定したパラメータでTweetsを入手し、表示するだけ。

Typeにはrecent、popular、mixedが指定可能。

tweets = tweepy.Cursor(api.search_tweets,
                       q='Minecraft',
                       tweet_mode='extended',
                       include_entities=True,
                       result_type='recent',
                       lang='ja' 
                       ).items(20)

for tweet in tweets:
    print(tweet.created_at)
    print(tweet.full_text) 
    print(tweet.retweet_count)
    print(tweet.favorite_count)

【Python】Tweepyを使用して、Twitterからツイート取得、自動投稿(画像付き) – V1.1、V2 両方対応 –|Zero-Cheese

このブログは、ネットや書籍上の情報、個人の体験や感想を中心にまとめたものです。 正確性を期していはいますが、間違い・誤訳等あるかもしれません。 当サイトの情報によって生じたいかなる損失について一切の責任を負わないものとします. あらかじめご了承ください。

プライバシーポリシー |〇利用規約 |〇問い合わせ