LoginSignup
6
7

More than 5 years have passed since last update.

PythonとTwitterAPIを使って過去のtweetを取得する

Last updated at Posted at 2018-01-24

Twitter認証キーの取得

環境設定や、認証キーの取得はこちらを参照してください。
PythonとTwitterAPI使ってtweetしてみる

タイムラインを取得する

各種認証キーをconfig.pyとして準備します。

config.py
CONSUMER_KEY = "**************"
CONSUMER_SECRET = "**************"
ACCESS_TOKEN = "**************"
ACCESS_TOKEN_SECRET = "**************"
timeline.py
# -*- coding:utf-8 -*-
import json, config
from requests_oauthlib import OAuth1Session

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"

params ={'count' : 100}
req = twitter.get(url, params = params)

if req.status_code == 200:
    timeline = json.loads(req.text)
    for tweet in timeline:
        print(tweet['user']['name']+'::'+tweet['text'])
        print(tweet['created_at'])
        print('----------------------------------------------------')
else:
    print("ERROR: %d" % req.status_code)

実行結果

スクリーンショット 2018-01-24 13.14.15.png

(/▽\) ハズカシ

6
7
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
6
7