我们在计划中遵循以下三个主要步骤:授权twitter API客户端。
向Twitter API发出GET请求以获取特定查询的推文。
解析推文。将每条推文分类为正面,负面或中立。
首先,我们创建一个TwitterClient类。该类包含与Twitter API交互和解析推文的所有方法。我们使用__init__函数来处理API客户端的身份验证。
在get_tweets函数中,我们使用:fetched_tweets = self.api.search(q = query,count = count)
调用Twitter API来获取推文。
在get_tweet_sentiment中,我们使用textblob模块。analysis = TextBlob(self.clean_tweet(tweet))
import re
import tweepy
from tweepy import OAuthHandler
from textblob import TextBlob
class TwitterClient(object):
'''
Generic Twitter Class for sentiment analysis.
'''
def __init__(self):
'''
Class constructor or initialization method.
'''
# keys and tokens from the Twitter Dev Console
consumer_key = 'XXXXXXXX