Python × Twitter API ツイート種類別にデータ取得する

## キーワードを含むツイート数
### 累計の場合

```search.py
import tweepy
import datetime

consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

tweets_data = []
total_tweets_data = []
keyword = '五反田'
since = []
until = []

# 日付をdatetimeオブジェクトに変更
sinceForm = '20210428'
untilForm = '20210501'
sinceDate = datetime.datetime.strptime(sinceForm, '%Y%m%d')
untilDate = datetime.datetime.strptime(
    untilForm, '%Y%m%d') + datetime.timedelta(days=1)
diff = (untilDate - sinceDate).days

# since, untilリストを作成
for i in range(diff):
    since.append(sinceDate + datetime.timedelta(days=i))

for i in range(diff):
    until.append(untilDate + datetime.timedelta(days=-i))
until.reverse()

# 検索実行
for i in range(diff):
    for tweet in tweepy.Cursor(
            api.search,
            tweet_mode='extended',
            q=keyword,
            lang='ja',
            since=since[i],
            until=until[i]).items():
        tweets_data.append(tweet.full_text.strip().replace('\n', '。') + '\n')
    total_tweets_data.append(len(tweets_data))

# キーワードを含むツイート数（累計）を表示
for i in total_tweets_data:
    print('-------------------------------------------')
    print(i)
```

#### 実行結果
[![Image from Gyazo](https://i.gyazo.com/996544d394b1b8a4262a497a4dd6cb3c.png)](https://gyazo.com/996544d394b1b8a4262a497a4dd6cb3c)

### 当日の場合

```search.py
import tweepy
import datetime

consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

tweets_data = []
total_tweets_data = []
day_tweets_data = []
keyword = '五反田'
since = []
until = []

# 日付をdatetimeオブジェクトに変更
sinceForm = '20210428'
untilForm = '20210501'
sinceDate = datetime.datetime.strptime(sinceForm, '%Y%m%d')
untilDate = datetime.datetime.strptime(
    untilForm, '%Y%m%d') + datetime.timedelta(days=1)
diff = (untilDate - sinceDate).days

# since, untilリストを作成
for i in range(diff):
    since.append(sinceDate + datetime.timedelta(days=i))

for i in range(diff):
    until.append(untilDate + datetime.timedelta(days=-i))
until.reverse()

# 検索実行
for i in range(diff):
    for tweet in tweepy.Cursor(
            api.search,
            tweet_mode='extended',
            q=keyword,
            lang='ja',
            since=since[i],
            until=until[i]).items():
        tweets_data.append(tweet.full_text.strip().replace('\n', '。') + '\n')
    total_tweets_data.append(len(tweets_data))

# キーワードを含むツイート数（当日）
for i in range(len(total_tweets_data)):
    if i > 0:
        day_tweets_data.append(
            total_tweets_data[i] - total_tweets_data[i - 1])
    else:
        day_tweets_data.append(total_tweets_data[i])

# キーワードを含むツイート数（当日）を表示
for i in day_tweets_data:
    print('-------------------------------------------')
    print(i)
```

#### 実行結果
[![Image from Gyazo](https://i.gyazo.com/3be0b25566717183a57e693085272520.png)](https://gyazo.com/3be0b25566717183a57e693085272520)

## キーワードを含むRT数
### 累計の場合

```search.py
import tweepy
import datetime

consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

tweets_data = []
total_tweets_data = []
day_tweets_data = []
exclude_RT_data = []
total_exclude_RT_data = []
day_exclude_RT_data = []
total_RT_data = []
day_RT_data = []
keyword = '五反田'
key = keyword.ljust(len(keyword) + 1) + "exclude:retweets"
since = []
until = []

# 日付をdatetimeオブジェクトに変更
sinceForm = '20210428'
untilForm = '20210501'
sinceDate = datetime.datetime.strptime(sinceForm, '%Y%m%d')
untilDate = datetime.datetime.strptime(
    untilForm, '%Y%m%d') + datetime.timedelta(days=1)
diff = (untilDate - sinceDate).days

# since, untilリストを作成
for i in range(diff):
    since.append(sinceDate + datetime.timedelta(days=i))

for i in range(diff):
    until.append(untilDate + datetime.timedelta(days=-i))
until.reverse()

# キーワードを含むツイート数（累計）を表示
for i in range(diff):
    for tweet in tweepy.Cursor(
            api.search,
            tweet_mode='extended',
            q=keyword,
            lang='ja',
            since=since[i],
            until=until[i]).items():
        tweets_data.append(tweet.full_text.strip().replace('\n', '。') + '\n')
    total_tweets_data.append(len(tweets_data))

# キーワードを含む && RTを除くツイート数（累計）を表示
for i in range(diff):
    for tweet in tweepy.Cursor(
            api.search,
            tweet_mode='extended',
            q=key,
            lang='ja',
            since=since[i],
            until=until[i]).items():
        total_exclude_RT_data.append(
            tweet.full_text.strip().replace(
                '\n', '。') + '\n')
    exclude_RT_data.append(len(total_exclude_RT_data))

# ツイート数 - RTを除くツイート数 = RT数
total_RT_data = [x - y for (x, y) in zip(total_tweets_data, exclude_RT_data)]

# キーワードを含むRT数（累計）を表示
for i in total_RT_data:
    print('-------------------------------------------')
    print(i)
```

#### 実行結果
[![Image from Gyazo](https://i.gyazo.com/c527bde9ca6cb2ad2ad0ae5077432b96.png)](https://gyazo.com/c527bde9ca6cb2ad2ad0ae5077432b96)

### 当日の場合

```search.py
import tweepy
import datetime

consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

tweets_data = []
total_tweets_data = []
day_tweets_data = []
exclude_RT_data = []
total_exclude_RT_data = []
day_exclude_RT_data = []
total_RT_data = []
day_RT_data = []
keyword = '五反田'
key = keyword.ljust(len(keyword) + 1) + "exclude:retweets"
since = []
until = []

# 日付をdatetimeオブジェクトに変更
sinceForm = '20210428'
untilForm = '20210501'
sinceDate = datetime.datetime.strptime(sinceForm, '%Y%m%d')
untilDate = datetime.datetime.strptime(
    untilForm, '%Y%m%d') + datetime.timedelta(days=1)
diff = (untilDate - sinceDate).days

# since, untilリストを作成
for i in range(diff):
    since.append(sinceDate + datetime.timedelta(days=i))

for i in range(diff):
    until.append(untilDate + datetime.timedelta(days=-i))
until.reverse()

# キーワードを含むツイート数（累計）を表示
for i in range(diff):
    for tweet in tweepy.Cursor(
            api.search,
            tweet_mode='extended',
            q=keyword,
            lang='ja',
            since=since[i],
            until=until[i]).items():
        tweets_data.append(tweet.full_text.strip().replace('\n', '。') + '\n')
    total_tweets_data.append(len(tweets_data))

# キーワードを含む && RTを除くツイート数（累計）を表示
for i in range(diff):
    for tweet in tweepy.Cursor(
            api.search,
            tweet_mode='extended',
            q=key,
            lang='ja',
            since=since[i],
            until=until[i]).items():
        total_exclude_RT_data.append(
            tweet.full_text.strip().replace(
                '\n', '。') + '\n')
    exclude_RT_data.append(len(total_exclude_RT_data))

# ツイート数 - RTを除くツイート数 = RT数
total_RT_data = [x - y for (x, y) in zip(total_tweets_data, exclude_RT_data)]

# キーワードを含むRT数（当日）
for i in range(len(total_RT_data)):
    if i > 0:
        day_RT_data.append(total_RT_data[i] - total_RT_data[i - 1])
    else:
        day_RT_data.append(total_RT_data[i])

# キーワードを含むRT数（当日）を表示
for i in day_RT_data:
    print('-------------------------------------------')
    print(i)
```

#### 実行結果
[![Image from Gyazo](https://i.gyazo.com/d9ffe480120353db2e9f1d09a414f6a5.png)](https://gyazo.com/d9ffe480120353db2e9f1d09a414f6a5)

## キーワードを含む引用数
### 累計の場合

```search.py
import tweepy
import datetime

consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

tweets_data = []
total_tweets_data = []
day_tweets_data = []
exclude_RT_data = []
total_exclude_RT_data = []
day_exclude_RT_data = []
total_RT_data = []
day_RT_data = []
quote_data = []
total_quote_data = []
day_quote_data = []
keyword = '五反田'
key = keyword.ljust(len(keyword) + 1) + "exclude:retweets"
since = []
until = []

# 日付をdatetimeオブジェクトに変更
sinceForm = '20210428'
untilForm = '20210501'
sinceDate = datetime.datetime.strptime(sinceForm, '%Y%m%d')
untilDate = datetime.datetime.strptime(
    untilForm, '%Y%m%d') + datetime.timedelta(days=1)
diff = (untilDate - sinceDate).days

# since, untilリストを作成
for i in range(diff):
    since.append(sinceDate + datetime.timedelta(days=i))

for i in range(diff):
    until.append(untilDate + datetime.timedelta(days=-i))
until.reverse()

# キーワードを含む引用数（累計）を表示
for i in range(diff):
    for tweet in tweepy.Cursor(
            api.search,
            tweet_mode='extended',
            q=keyword,
            lang='ja',
            since=since[i],
            until=until[i]).items():
        quote_data.append(tweet.is_quote_status)
    total_quote_data.append(quote_data.count(True))

# キーワードを含む引用数（累計）を表示
for i in total_quote_data:
    print('-------------------------------------------')
    print(i)
```

#### 実行結果
[![Image from Gyazo](https://i.gyazo.com/1766fb0d65cc261a8b2a61d4a00cdaeb.png)](https://gyazo.com/1766fb0d65cc261a8b2a61d4a00cdaeb)

### 当日の場合

```search.py
import tweepy
import datetime

consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

tweets_data = []
total_tweets_data = []
day_tweets_data = []
exclude_RT_data = []
total_exclude_RT_data = []
day_exclude_RT_data = []
total_RT_data = []
day_RT_data = []
quote_data = []
total_quote_data = []
day_quote_data = []
keyword = '五反田'
key = keyword.ljust(len(keyword) + 1) + "exclude:retweets"
since = []
until = []

# 日付をdatetimeオブジェクトに変更
sinceForm = '20210428'
untilForm = '20210501'
sinceDate = datetime.datetime.strptime(sinceForm, '%Y%m%d')
untilDate = datetime.datetime.strptime(
    untilForm, '%Y%m%d') + datetime.timedelta(days=1)
diff = (untilDate - sinceDate).days

# since, untilリストを作成
for i in range(diff):
    since.append(sinceDate + datetime.timedelta(days=i))

for i in range(diff):
    until.append(untilDate + datetime.timedelta(days=-i))
until.reverse()

# キーワードを含む引用数（累計）を表示
for i in range(diff):
    for tweet in tweepy.Cursor(
            api.search,
            tweet_mode='extended',
            q=keyword,
            lang='ja',
            since=since[i],
            until=until[i]).items():
        quote_data.append(tweet.is_quote_status)
    total_quote_data.append(quote_data.count(True))

# キーワードを含む引用数（当日）
for i in range(len(total_quote_data)):
    if i > 0:
        day_quote_data.append(total_quote_data[i] - total_quote_data[i - 1])
    else:
        day_quote_data.append(total_quote_data[i])

# キーワードを含む引用数（当日）を表示
for i in day_quote_data:
    print('-------------------------------------------')
    print(i)
```

#### 実行結果
[![Image from Gyazo](https://i.gyazo.com/d53f819fdae31bdd2963cf1831709a29.png)](https://gyazo.com/d53f819fdae31bdd2963cf1831709a29)

## 参考文献
 - [TwitterのSearch APIの使い方で混乱したのでまとめてみる](https://www.kwbtblog.com/entry/2020/01/28/045248)
 - [Twitter Rest APIで取得できるデータのまとめ](https://syncer.jp/Web/API/Twitter/REST_API/Object/)
  - [Django + TwitterAPIでアプリを作成する](https://note.com/yukiaofficial/n/nd56257767da2)
 - [DjangoにPlotlyのグラフを表示する](https://zenn.dev/driller/articles/6c048f953c2bc71916fb)
 - [TweepyでTwitterの検索, いいね, リツイート](https://qiita.com/pontyo4/items/4ef6c9cb8e15a01a3129)
 - [【Tweepy】TwitterAPIでのデータ収集で少し間違えた話](https://qiita.com/yamatia/items/455f77ee98a7f22afff0)
 - [pythonのmatplotlibでグラフを作成してみた](https://qiita.com/robitan/items/12d2dc99edd3c31848dc)
 - [Plotlyで作るグラフをDjangoで使う](https://qiita.com/taku_hito/items/62aeb4271614f6f0347f)
 - [[Python] Plotlyでぐりぐり動かせるグラフを作る](https://qiita.com/inoory/items/12028af62018bf367722)
 - [Plotlyで折れ線グラフや複合グラフ](https://qiita.com/haru1977/items/87fba196e506f9fe225d)
 - [【Python×TwitterAPI】Twitterクライアント作成までの道【APIで遊ぶ編】](https://tech-blog.rakus.co.jp/entry/20201106/api)