【发布时间】:2017-02-19 05:03:32
【问题描述】:
我最近一直在使用 python 中的 tweepy 库。我编写了一个通过推文流式传输的程序。我使用字符串作为过滤器。 我已经根据字符串所代表的情绪为列表中的每个字符串分配了一个特定的变量。
class listener(StreamListener):
def on_status(self, status):
try:
print status
return True
except BaseException, e:
print 'failed on status, ', str(e)
time.sleep(5)
if '' in status.text.lower() and 'retweeted_status' not in status:
print status.text
print status.coordinates
def on_error(self, status):
print status
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
twitterStream = Stream(auth, listener())
api = tweepy.API(auth)
happy=["I love","I'm so happy","feeling joyful","feeling awesome"]
sad=["I'm depressed","I'm very sad","It is painful","feeling terible"]
angry=["I'm furious","wtf!","I'm so pissed off","I'm angry"]
shocked=["rip","omg","I can't believe it","I'm shocked"]
romantic=["I love you","today is my anniversary","feeling romantic","I'm dating"]
twitterStream.filter(track=sad)(track=happy)(track=angry)(track=shocked)(track=romantic)
mood_happy=0
mood_sad=0
mood_angry=0
mood_shocked=0
mood_romantic=0
我想做的是每次来自任何变量的字符串出现在推文行中时,我希望mood_n的值加起来为1。例如,如果短语“Feeling Joyful”在流中出现5次.我想要mood_happy = 5的值。我该怎么办?
对不起,我知道我不应该发布这样的查询,但我已经在谷歌上搜索了好几个小时的解决方案,但没有找到任何关于此的信息。 :(
【问题讨论】:
标签: python stream twitter-oauth tweepy