Python × Twitter API 検索 && 複合グラフ表示

## 特定ユーザのツイート５件

```search.py
import tweepy

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)

# 特定のユーザー
Account = "non_0131"
tweets = api.user_timeline(Account, count=5)
for tweet in tweets:
    print('-------------------------------------------')
    print('tweetId : ', tweet.id)
    print(tweet.text)
```

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

## キーワード検索 5件

```search.py
import tweepy

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)

keyword = 'Python'

# キーワード検索
for tweet in api.search(q=keyword, count=5):
    print('-------------------------------------------')
    print('name:' + tweet.user.name)
    print(tweet.text)
```

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

## 特定のキーワードを含むツイート件数

```search.py
import tweepy

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)

# ツイート件数 ※最大10件まで
keyword = '五反田'
tweets_data = []

for tweet in tweepy.Cursor(
    api.search,
    q=keyword,
    tweet_mode='extended',
        lang='ja').items(10):
    tweets_data.append(tweet.full_text.strip().replace('\n', '。') + '\n')

print(len(tweets_data))
```

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

## 特定のキーワードを含むツイート件数（期間指定）

```search.py
import tweepy

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 = []
keyword = '五反田'
since = "2021-04-23 00:00:00"
until = "2021-04-24 00:00:00"

for tweet in tweepy.Cursor(api.search,
                           q=keyword,
                           tweet_mode='extended',
                           lang='ja',
                           since=since,
                           until=until).items():
    tweets_data.append(tweet.full_text.strip().replace(
        '\n', '。') + '\n')

print(len(tweets_data))
```

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

## Plotly 複合グラフ表示

```search.py
import plotly.graph_objects as go

fig = go.Figure()
X = ["2021/4/18", "2021/4/19", "2021/4/20"]
Y = [50, 100, 1000]
Z = [10, 70, 100]

fig.add_trace(go.Bar(x=X, y=Y, name="累計"))

fig.add_trace(go.Scatter(x=X, y=Z, name="当日"))

fig.update_xaxes(title="日時")
fig.update_yaxes(title="投稿数（累計）")
fig.update_layout(font={"family": "Meiryo", "size": 20})
fig.update_layout(showlegend=True)
fig.update_layout(width=800, height=600)
fig.update_layout(template="plotly_white")
fig.show()
```

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


## 参考文献
 - [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)