0% found this document useful (0 votes)
12 views21 pages

Chapter3 4

This document discusses how to access and stream data from the Twitter API using the Tweepy Python package. It covers authentication, defining a stream listener class, and filtering tweets by keywords.

Uploaded by

adnanbashir2187
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views21 pages

Chapter3 4

This document discusses how to access and stream data from the Twitter API using the Tweepy Python package. It covers authentication, defining a stream listener class, and filtering tweets by keywords.

Uploaded by

adnanbashir2187
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

The Twitter API and

Authentication
I N T E R M E D I AT E I M P O R T I N G D ATA I N P Y T H O N

Hugo Bowne-Anderson
Data Scientist at DataCamp
Herein, you’ll learn
How to stream data from the Twi er API

How to lter incoming tweets for keywords

About API Authentication and OAuth

INTERMEDIATE IMPORTING DATA IN PYTHON


Herein, you’ll learn
How to stream data from the Twi er API

How to lter incoming tweets for keywords

About API Authentication and OAuth

How to use the Tweepy Python package

INTERMEDIATE IMPORTING DATA IN PYTHON


Access the Twitter API

INTERMEDIATE IMPORTING DATA IN PYTHON


Access the Twitter API

INTERMEDIATE IMPORTING DATA IN PYTHON


Access the Twitter API

INTERMEDIATE IMPORTING DATA IN PYTHON


Access the Twitter API

INTERMEDIATE IMPORTING DATA IN PYTHON


Twitter has a number of APIs

INTERMEDIATE IMPORTING DATA IN PYTHON


Twitter has a number of APIs

INTERMEDIATE IMPORTING DATA IN PYTHON


Twitter has a number of APIs

INTERMEDIATE IMPORTING DATA IN PYTHON


Twitter has a number of APIs

INTERMEDIATE IMPORTING DATA IN PYTHON


Twitter has a number of APIs

INTERMEDIATE IMPORTING DATA IN PYTHON


Tweets are returned as JSONs

INTERMEDIATE IMPORTING DATA IN PYTHON


Tweets are returned as JSONs

INTERMEDIATE IMPORTING DATA IN PYTHON


Using Tweepy: Authentication handler
tw_auth.py

import tweepy, json


access_token = "..."
access_token_secret = "..."
consumer_key = "..."
consumer_secret = "..."
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

INTERMEDIATE IMPORTING DATA IN PYTHON


Tweepy: define stream listener class
st_class.py

class MyStreamListener(tweepy.StreamListener):
def __init__(self, api=None):
super(MyStreamListener, self).__init__()
self.num_tweets = 0
self.file = open("tweets.txt", "w")
def on_status(self, status):
tweet = status._json
self.file.write(json.dumps(tweet) + '\\n')
tweet_list.append(status)
self.num_tweets += 1
if self.num_tweets < 100:
return True
else:
return False
self.file.close()

INTERMEDIATE IMPORTING DATA IN PYTHON


Using Tweepy: stream tweets!!
tweets.py

# Create Streaming object and authenticate


l = MyStreamListener()
stream = tweepy.Stream(auth, l)
# This line filters Twitter Streams to capture data by keywords:
stream.filter(track=['apples', 'oranges'])

INTERMEDIATE IMPORTING DATA IN PYTHON


Let's practice!
I N T E R M E D I AT E I M P O R T I N G D ATA I N P Y T H O N
Final Thoughts
I N T E R M E D I AT E I M P O R T I N G D ATA I N P Y T H O N

Hugo Bowne-Anderson
Data Scientist at DataCamp
What you’ve learned:
Importing text les and at les

Importing les in other formats

Writing SQL queries

Ge ing data from relational databases

Pulling data from the web

Pulling data from APIs

INTERMEDIATE IMPORTING DATA IN PYTHON


Let's practice!
I N T E R M E D I AT E I M P O R T I N G D ATA I N P Y T H O N

You might also like