filmov
tv
Twitter Streaming API - Python & Code!

Показать описание
CODE POSTED BELOW! Click "show more"
Python 3.4 (probably compatible with other versions)
Original Guide & extra Python program options (3rd party website):
Follow the Twitter API instructions on the guide above. This link will point you to where you'll need to set up a free Twitter account and get access to their free Twitter Python streaming API.
The Code! Be sure to change the "APIrelatedkeys" to your API keys from Twitter Streaming API:
#Import the necessary methods from tweepy library
from tweepy import OAuthHandler
from tweepy import Stream
import json
#Variables that contains the user credentials to access Twitter API
access_token = "APIrelatedkeys"
access_token_secret = "APIrelatedkeys"
consumer_key = "APIrelatedkeys"
consumer_secret = "APIrelatedkeys"
#This is a basic listener that just prints received tweets to stdout.
class StdOutListener(StreamListener):
def on_data(self, data):
print (decoded['user']['screen_name'], decoded['text'].encode('ascii', 'ignore'))
return True
def on_error(self, status):
print(status)
if __name__ == '__main__':
#This handles Twitter authetification and the connection to Twitter Streaming API
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
stream = Stream(auth, l)
#This line filter Twitter Streams to capture data by the keyword: 'python'
0:00 - Introduction to setting up Twitter streaming API via Python
0:24 - Setting up a Twitter streaming API account and obtaining API keys
1:36 - Code modification for cleaner tweet output
2:10 - Customizing search keywords in the program
2:32 - Running the program and concluding remarks
Python 3.4 (probably compatible with other versions)
Original Guide & extra Python program options (3rd party website):
Follow the Twitter API instructions on the guide above. This link will point you to where you'll need to set up a free Twitter account and get access to their free Twitter Python streaming API.
The Code! Be sure to change the "APIrelatedkeys" to your API keys from Twitter Streaming API:
#Import the necessary methods from tweepy library
from tweepy import OAuthHandler
from tweepy import Stream
import json
#Variables that contains the user credentials to access Twitter API
access_token = "APIrelatedkeys"
access_token_secret = "APIrelatedkeys"
consumer_key = "APIrelatedkeys"
consumer_secret = "APIrelatedkeys"
#This is a basic listener that just prints received tweets to stdout.
class StdOutListener(StreamListener):
def on_data(self, data):
print (decoded['user']['screen_name'], decoded['text'].encode('ascii', 'ignore'))
return True
def on_error(self, status):
print(status)
if __name__ == '__main__':
#This handles Twitter authetification and the connection to Twitter Streaming API
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
stream = Stream(auth, l)
#This line filter Twitter Streams to capture data by the keyword: 'python'
0:00 - Introduction to setting up Twitter streaming API via Python
0:24 - Setting up a Twitter streaming API account and obtaining API keys
1:36 - Code modification for cleaner tweet output
2:10 - Customizing search keywords in the program
2:32 - Running the program and concluding remarks
Комментарии