Twitter API를 통한 데이터 수집

Python으로 소셜 미디어 데이터 분석하기

Alex Hanna

Computational Social Scientist

Twitter API

  • API: 애플리케이션 프로그래밍 인터페이스
    • 데이터 접근 방법
  • Twitter API
    • Search API
    • Ads API
    • Streaming API
Python으로 소셜 미디어 데이터 분석하기

Streaming API

  • Streaming API
    • 실시간 트윗
  • Filter 엔드포인트
    • 키워드
    • 사용자 ID
    • 위치
  • Sample 엔드포인트
    • 무작위 샘플
Python으로 소셜 미디어 데이터 분석하기

tweepy를 사용한 데이터 수집

  • tweepy
    • Streaming API 접근을 위한 Python 패키지
Python으로 소셜 미디어 데이터 분석하기

SListener

from tweepy import Stream
import time

class SListener(Stream):
    def __init__(self, api = None):
        self.output  = open('tweets_%s.json' %
            time.strftime('%Y%m%d-%H%M%S'), 'w')
        self.api = api or API()
    ...
Python으로 소셜 미디어 데이터 분석하기

tweepy 인증

from tweepy import OAuthHandler
from tweepy import API 

auth = OAuthHandler(consumer_key, consumer_secret)

auth.set_access_token(access_token, access_token_secret)
api = API(auth)
Python으로 소셜 미디어 데이터 분석하기

tweepy로 데이터 수집하기

from tweepy import Stream

listen = SListener(api)

stream = Stream(auth, listen)
stream.sample()
Python으로 소셜 미디어 데이터 분석하기

연습해 봅시다!

Python으로 소셜 미디어 데이터 분석하기

Preparing Video For Download...