【Python】Twitter APIで取得したユーザーのデータを表示する関数

Pythonでtweepyを使い、Twitter API接続してユーザー情報を取得する際の関数の一覧を紹介します。

Twitter APIで取得できる情報

よく使いそうなやつをピックアップします。

import tweepy

apiKey = 'API Keyを入力してください'
apiSerect = 'API Serectを入力してください'
accessToken = 'Access Token を入力してください'
tokenSerect = 'Token Serectを入力してください'

auth = tweepy.OAuthHandler(apiKey, apiSerect)
auth.set_access_token(accessToken, tokenSerect)
api = tweepy.API(auth)
information = key.api.user_timeline(screen_name='YusukeGoto_')[0]

print(information.text)

informationの中にユーザー情報が入っていると想定して紹介します。
どうやらinformationなど自分で決めるのではなくstatusが使われていることが多いですね…

実際に自分のTwitterの情報を取得してみました

関数名 information.text information.user.name information.user.screen_name information.user.location information.user.description information.user.followers_count information.user.friends_count information.user.listed_count information.user.created_at information.user.statuses_count information.lang information.user.protected
内容 ツイート内容 表示される名前 ID プロフィールに設定している位置 プロフィールに設定している自己紹介文 フォロワー数 フォロー数 リストの数 Twitterを始めた日 ツイート数 言語 アカウントに鍵がかかっているか
千条の滝
#箱根 #小涌谷 #千条の滝#THETA #THETA360 #VRカメラ #360camera #360photo #360view #ricohtheta #littleplanet #photo360… https://t.co/i9DZkVdrWo
yusukegoto.eth YusukeGoto_ Japan InstagramやYouTubeにVRカメラで撮影した自然の写真や動画を投稿してます / Twitterやblogでは仮想通貨関連の情報を発信 #ビットコイン #BTC 779 120 0 2012-11-24 02:03:31+00:00 1167 ja TrueもしくはFalse

フォロワー数やフォロー数は数値型で、Twitterを始めた日などは日付の型なので、printする際はstr()で型変換しましょう

print(str(information.user.followers_count))

スクレイピングした内容を取得してTwitterに投稿するプログラムを作成しています。

©Copyright2022 FICH-LABO.All Rights Reserved.