2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Python3】tweepy.Streamでツイートを全文取得する(2022年3月時点)

Last updated at Posted at 2022-03-31

tweepy v4からStream周りの仕様が色々と変わっており、紹介されている方が少ない気がしたので備忘録として書いておきます。

環境

  • Python 3.9.10
  • tweepy 4.6.0

ソースコード

timeline.py
import tweepy

class StreamListener(tweepy.Stream):
    # ツイート取得時の動作
    def on_status(self, status):
        # ツイートの全文を取得する
        if 'extended_tweet' in status._json:
            text = status._json['extended_tweet']['full_text']
        elif 'extended_tweet' not in status._json:
            text = status.text

        # ツイートを出力
        tw = '[@{username}] {text}\n'.format(username = status.user.screen_name, text = text)
        print(tw)

streamlistener = StreamListener('ConsumerKey', 'ConsumerSecret', 'AccessToken', 'AccessTokenSecret')

streamlistener.filter(track=['python'])

参考

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?