2
3

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 5 years have passed since last update.

AIIT(産業技術大学院大学)Advent Calendar 2017

Day 16

TwitterAPIを使ってタイムラインを収集

Last updated at Posted at 2017-12-26

TwitterAPIを使ってデータ収集したときの忘備録
##APIを使ってタイムラインを収集

tweetdeta.py
from requests_oauthlib import OAuth1Session
import json

CK = 'ここに入力する(この文は消してください)' # Consumer Key
CS = 'ここに入力する(この文は消してください)' # Consumer Secret
AT = 'ここに入力する(この文は消してください)' # Access Token
AS = 'ここに入力する(この文は消してください)' # Accesss Token Seceret

url = "https://api.twitter.com/1.1/statuses/home_timeline.json" #タイムライン取得用のURL

params = {'since':2017-12-24,'count': 100}

twitter = OAuth1Session(CK, CS, AT, AS)
req = twitter.get(url, params = params)

if req.status_code == 200:
    
    timeline = json.loads(req.text)
    for tweet in timeline:
        print(tweet["text"])

else:
    print ("Error: %d" % req.status_code)

収集できるデータの自分がフォローしているユーザーのタイムラインしかできない。
※開発用のTwitter
実行結果
image.png

2
3
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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?