4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

#Twitter #API でページめくりをして200件超のツイートを取得する #python スクリプトの例

Last updated at Posted at 2019-02-16

#Twitter #API でページめくりをして200件超のツイートを取得する #python スクリプトの例

準備

config.py に各種キーなどを記載しておく

CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_TOKEN = ''
ACCESS_TOKEN_SECRET = ''

Point

API のパラメータにmax_idという値が指定できる
max_idにツイートのidを渡すと、取得する範囲をずらせる
APIを叩いたときに得られる一番古いツイートIDをmax_idに指定すれば、その次のページが得られる

Script

環境変数 COUNT で一回あたりの取得ツイート数を指定できるようにした
環境変数 ROUND でAPIを叩く回数、ページネーション回数を指定できるようにした

# https://developer.twitter.com/en/docs/tweets/timelines/api-reference/get-statuses-user_timeline.html

import json, config, os, re
from requests_oauthlib import OAuth1Session
import time
import datetime
from datetime import timedelta
 
CK = config.CONSUMER_KEY
CS = config.CONSUMER_SECRET
AT = config.ACCESS_TOKEN
ATS = config.ACCESS_TOKEN_SECRET
twitter = OAuth1Session(CK, CS, AT, ATS)

url = "https://api.twitter.com/1.1/statuses/user_timeline.json"
last_id = ''

def response(max_id):
  api_params = {
    'trim_user' : True,
    'exclude_replies' : True,
    'tweet_mode' : 'extended',
  }
  
  if max_id:
    api_params['max_id'] = max_id

  if os.environ.get('COUNT'):
    api_params['count'] = os.environ.get('COUNT')

  res = twitter.get(url, params = api_params)
  
  if res.status_code != 200:  
      print("Failed: %d" % res.status_code)
      exit()

  return res

def results(timelines):
  line_results = []

  for line in timelines:
      # if you need tweet text then use line['full_text'] 
      line_results.append(line['created_at'] + " : " + str(line['id']))
      last_id = line['id']

  return last_id, line_results

round = int(os.environ.get('ROUND')) or 5

for i in range(0, round):
  res = response(last_id)
  timelines = json.loads(res.text)
  
  last_id, timeline_results = results(timelines)
  timeline_results.pop()

  for result in timeline_results:
    print(result)

Result Example

Yumas-MacBook-Air:YumaInaura yumainaura$ COUNT=10 ROUND=5 python twitter/pagination-timeline.py

Sat Feb 16 15:43:56 +0000 2019 : 1096797331914027008
Sat Feb 16 15:22:49 +0000 2019 : 1096792020868227072
Sat Feb 16 15:21:52 +0000 2019 : 1096791781453135873
Sat Feb 16 14:40:15 +0000 2019 : 1096781307068047360
Sat Feb 16 14:36:38 +0000 2019 : 1096780398053314562
Sat Feb 16 14:24:39 +0000 2019 : 1096777381086285824
Sat Feb 16 14:20:45 +0000 2019 : 1096776397945634816
Sat Feb 16 14:20:41 +0000 2019 : 1096776384930664448
Sat Feb 16 14:16:05 +0000 2019 : 1096775227294380032
Sat Feb 16 14:12:34 +0000 2019 : 1096774342468227076
Sat Feb 16 14:01:05 +0000 2019 : 1096771448859836417
Sat Feb 16 13:50:54 +0000 2019 : 1096768887549390848
Sat Feb 16 13:35:43 +0000 2019 : 1096765068304207872
Sat Feb 16 13:35:17 +0000 2019 : 1096764957872418817
Sat Feb 16 13:29:04 +0000 2019 : 1096763391824191488
Sat Feb 16 13:17:35 +0000 2019 : 1096760503546736640
Sat Feb 16 13:17:33 +0000 2019 : 1096760493979533314
Sat Feb 16 13:12:45 +0000 2019 : 1096759285063663617
Sat Feb 16 12:54:43 +0000 2019 : 1096754750509338625
Sat Feb 16 12:51:08 +0000 2019 : 1096753845600890881
Sat Feb 16 12:40:52 +0000 2019 : 1096751265021718528
Sat Feb 16 12:39:31 +0000 2019 : 1096750923202781184
Sat Feb 16 12:39:27 +0000 2019 : 1096750906597552130
Sat Feb 16 12:39:25 +0000 2019 : 1096750897672052738
Sat Feb 16 12:34:22 +0000 2019 : 1096749627729108992
Sat Feb 16 12:31:02 +0000 2019 : 1096748789677715456
Sat Feb 16 12:00:59 +0000 2019 : 1096741227821551616

Ref

GET statuses/user_timeline — Twitter Developers

Original by Github issue

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

4
5
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
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?