Thu thập dữ liệu qua Twitter API

Phân tích dữ liệu mạng xã hội bằng Python

Alex Hanna

Computational Social Scientist

Twitter API

  • API: Application Programming Interface
    • Cách truy cập dữ liệu
  • Twitter APIs
    • Search API
    • Ads API
    • Streaming API
Phân tích dữ liệu mạng xã hội bằng Python

Streaming API

  • Streaming API
    • Tweet theo thời gian thực
  • Filter endpoint
    • Từ khóa
    • User ID
    • Vị trí
  • Sample endpoint
    • Mẫu ngẫu nhiên
Phân tích dữ liệu mạng xã hội bằng Python

Dùng tweepy để thu thập dữ liệu

  • tweepy
    • Gói Python để truy cập Streaming API
Phân tích dữ liệu mạng xã hội bằng 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()
    ...
Phân tích dữ liệu mạng xã hội bằng Python

Xác thực 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)
Phân tích dữ liệu mạng xã hội bằng Python

Thu thập dữ liệu với tweepy

from tweepy import Stream

listen = SListener(api)

stream = Stream(auth, listen)
stream.sample()
Phân tích dữ liệu mạng xã hội bằng Python

Cùng luyện tập nào!

Phân tích dữ liệu mạng xã hội bằng Python

Preparing Video For Download...