LoginSignup
0
1

More than 3 years have passed since last update.

Twitter のフォロアーを取得 (Python3)

Last updated at Posted at 2018-11-28

Python3 で Twitter のフォロアーを取得する方法です。

get_followers.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#   get_followers.py
# --------------------------------------------------------------------
import sys
import json
from config_twitter import config_twitter_proc

# --------------------------------------------------------------------
def get_info_proc(twitter,id):
    url = "https://api.twitter.com/1.1/users/show.json"
    params = {'user_id': id}
    req = twitter.get(url, params = params)
    if req.status_code == 200:
        json_str = req.text
        dict_aa = json.loads(json_str)
        str_out = str(id) + "\t" + dict_aa['name'] + "\t" \
            + dict_aa['screen_name'] + "\t" \
            + dict_aa['location'] + "\t"
        print (str_out)
#
# --------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")

url = "https://api.twitter.com/1.1/followers/ids.json"

params = {'screen_name': "ekzemplaro",
    'count': 100}

twitter = config_twitter_proc()

req = twitter.get(url, params = params)
print(req)
#
if req.status_code == 200:
    print ("OK")
    json_str = req.text
    dict_aa = json.loads(json_str)
    print (dict_aa['ids'])
    for id in dict_aa['ids']:
        get_info_proc(twitter,id)
else:
    print ("Error: %d" % req.status_code)
#
sys.stderr.write("*** 終了 ***\n")
# --------------------------------------------------------------------

config_twitter.py はこちらのものと同じです。
Twitter に投稿する (Python3)

次のバージョンで確認しました。

$ python --version
Python 3.7.3
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1