Community Data Science Course (Spring 2016)/Day 6 Lecture

From CommunityData
< Community Data Science Course (Spring 2016)
Revision as of 02:15, 12 May 2016 by Guyrt (talk | contribs) (Created page with "''' Code to get a single Tweet''' <source type="python"> import encoding_fix import tweepy from twitter_authentication import CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, AC...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Code to get a single Tweet

import encoding_fix
import tweepy
from twitter_authentication import CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

api = tweepy.API(auth)

public_tweets = api.home_timeline(count=1)

single_tweet = public_tweets[0]

print(single_tweet._json)

All the lines up to tweepy.API(auth) are "boilerplate" meaning they are always the same. They take care of authenticating with Python.

Once we have access to Twitter's API, we can use it to do many things. The example above gets the first tweet from your timeline. You can print it as json by doing public_tweets[0]._json.