[スポンサーリンク]
Python3でTwitterAPI使用する準備
Python3でTwitterAPI使用する準備として、下記を行いました
①APIキーとトークンの取得
②ライブラリのインストール

【Python3】【TwitterAPI】PythonとTwitterAPIを触ってみる準備
TwitterAPIとは先日Python3を使用出来る環境を構築しました勉強として、Pythonで簡単なプログラミングをしてみたいと思います何を作るか迷ったのですが、良く利用しているTwitterを操作して...
[スポンサーリンク]
フレンドを取得 GET friends/ids API
TwitterAPIでフレンドを取得するには、GET friends/ids APIを使用します

GET friends/ids
パラメータ
GET friends/ids APIのパラメータは以下です
各項目について、メモを残しておきます
記載内容一切保証出来ません
| 項目名 | 要否 | 個人的メモ | 
| user_id | optional | 対象のアカウントをユーザIDで指定(screen_nameか一方を指定) | 
| screen_name | optional | 対象のアカウントをスクリーンネームで指定(user_idか一方を指定) | 
| cursor | semi-optional | ページングのカーソルを指定 | 
| stringify_ids | optional | 取得したIDの型を指定。Trueの場合文字列型、Falseの場合数値型。 | 
| count | optional | 取得するフォロワーの数を指定。 | 
GET friends/ids API サンプルコード
・GET friends/ids APIのURLを設定
| 
					 1  | 
						GetFriendsIdsApiUrl = 'https://api.twitter.com/1.1/friends/ids.json'  | 
					
・OAuth認証でTwitterとセッション接続
| 
					 1  | 
						GetFriendsIdsApiSession = OAuth1Session(ConsumerAPIKey, ConsumerAPIsecretkey, AccsessToken, AceestokenSecret)  | 
					
・Sessionクラスのgetを使って、GET friends/ids APIのレスポンスを取得
| 
					 1  | 
						GetFriendsIdsResponse = GetFriendsIdsApiSession.get(GetFriendsIdsApiUrl, params = GetFriendsIdsParameters)  | 
					
・カーソル設定
 GET friends/ids APIはページングで、レスポンスが返ってきます
 初期カーソルは-1となっています
 レスポンスのnext_cursor_strに、次のページを指定するカーソルが返ってきます
 レスポンスが最終ページの場合は、next_cursor_strに0が返ってくる仕様です
 したがって、next_cursor_strが0になるまでwhileループすることで、5000フレンド以上の場合でも全フレンドを取得出来ます
| 
					 1 2 3 4 5  | 
						Cursor = '-1' while  Cursor != '0':     【略】         if GetFriendsIdsResponse.status_code == 200:                 Cursor = GetFriendsIdsResult['next_cursor_str']  | 
					
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43  | 
						# coding: utf_8 # ライブラリインポート import json from requests_oauthlib import OAuth1Session # APIキー&トークン設定 ConsumerAPIKey = 【ConsumerKey】 ConsumerAPIsecretkey = 【ConsumerSecretKey】 AccsessToken = 【AccessToken】 AceestokenSecret = 【AccessTokenSecret】 # フォロワー取得対象設定 AccountUserId = '【ユーザID】' # フレンドリストパラメータ設定 Cursor = '-1' GetFriendsList = [] GetFriendsIdsApiSession = OAuth1Session(ConsumerAPIKey, ConsumerAPIsecretkey, AccsessToken, AceestokenSecret) GetFriendsIdsApiUrl = 'https://api.twitter.com/1.1/friends/ids.json' while  Cursor != '0':         GetFriendsIdsParameters = {                 'user_id' : AccountUserId                 ,'screen_name' : ''                 ,'cursor' : Cursor                 ,'stringify_ids' : ''                 ,'count' : '5000'         }         # フレンド取得         GetFriendsIdsResponse = GetFriendsIdsApiSession.get(GetFriendsIdsApiUrl, params = GetFriendsIdsParameters)         # 取得成功時はカーソルを進める         if GetFriendsIdsResponse.status_code == 200:                 GetFriendsIdsResult = json.loads(GetFriendsIdsResponse.text)                 GetFriendsList.extend(GetFriendsIdsResult['ids'])                 Cursor = GetFriendsIdsResult['next_cursor_str']         else:                 print(GetFriendsIdsResponse)                 exit() print('【フレンド数】' + str(len(GetFriendsList))) print(GetFriendsList)  | 
					
[スポンサーリンク]
ユーザIDを確認する方法
ユーザIDを調べる方法については、以下に記録しています

【Twitter】ユーザIDを確認する方法
TwitterアカウントのユーザIDを調べる方法PythonでTwitterAPIを触ってみる際に、ユーザIDを指定したい場合がありますスクリーンネーム(@hoge形式)は、表示されるのでアプリや...
[スポンサーリンク]

  
  
  
  
コメント