Last active
July 16, 2018 14:34
-
-
Save taikomegane/eb8ad011575ec1666e9a9e46d252dd57 to your computer and use it in GitHub Desktop.
自分のフォロワーのフォロー数、フォロワー数を取得する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 必要なライブラリをインポート | |
import tweepy | |
import matplotlib.pyplot as plt | |
# 各種キーをセット | |
CONSUMER_KEY = 'xxxxxxxxxxxxxxxx' | |
CONSUMER_SECRET = 'xxxxxxxxxxxxxxxx' | |
ACCESS_TOKEN = 'xxxxxxxxxxxxxxxx' | |
ACCESS_SECRET = 'xxxxxxxxxxxxxxxx' | |
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) | |
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET) | |
#APIインスタンスを作成 | |
api = tweepy.API(auth) | |
#自分のフォロワーのIDを取得 | |
ids = api.followers_ids("taikomegane") | |
#フォロワー数とフォロー数を格納するリストを用意 | |
follower_list = [] | |
friend_list = [] | |
#取得したIDそれぞれについて処理を繰り返す | |
for id in ids: | |
#IDからユーザ情報を取得 | |
user_info = api.get_user(id) | |
#ユーザ情報からフォロワー数を取得、格納 | |
follower = int(user_info.followers_count) | |
follower_list.append(follower) | |
#ユーザ情報からフォロー数を取得、格納 | |
friend = int(user_info.friends_count) | |
friend_list.append(friend) | |
# フォロワー数、フォロー数のヒストグラムをそれぞれ描画 | |
plt.hist(follower_list,bins=30) | |
plt.hist(friend_list,bins=30) | |
# フォロワー数、フォロー数の散布図を描画 | |
plt.scatter(follower_lists,friend_list) | |
print("終了しました") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment