我最近一直在使用python中的tweepy库。我写了一个程序,通过推文流。我已经使用字符串作为过滤器。 我根据字符串表示的情绪为列表中的每个字符串分配了一个特定变量。
I don't know why you have written this complex css. It can be possible by some easy css coding.
<style>
div.shadow {
width: 100%;
float: left;
text-align: center;
}
img.logo {
}
header {
text-align: center;
color: black;
text-decoration: none;
font-size: 40px;
font-family: 'existencelight';
width: 100%;
float: left;
}
</style>
我想要做的是每次来自任何变量的字符串出现在推文行中我希望mood_n的值加起来为1.所以例如,如果短语“感觉快乐”。在流中出现5次。我想要mood_happy = 5的值。我怎么能这样做?
对不起,我知道我不应该发布此类查询但是我已经在很长时间内在Google上搜索解决方案了但是还没有在此发现了一点信息。 :(
答案 0 :(得分:0)
有一个优化的地方,但基本上看起来像:
import time
from tweepy import StreamListener, OAuthHandler, Stream
twitter_access = {
"consumer_key" : "enter_consumer_key",
"consumer_secret" : "enter_consumer_secret",
"access_token" : "enter_access_token",
"access_token_secret": "enter_access_token_secret",
}
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"]
mood_happy = 0
mood_sad = 0
mood_angry = 0
mood_shocked = 0
mood_romantic = 0
class Listener(StreamListener):
def on_status(self, status):
global mood_happy, mood_sad, mood_angry, mood_shocked, mood_romantic
try:
# print status
tweet_text = status.text
for mood_n_score in [[happy, 'mood_happy'], [sad, 'mood_sad'], [angry, 'mood_angry'],
[shocked, 'mood_shocked'], [romantic, 'mood_romantic']]:
lst_mood = mood_n_score[0]
type_mood = mood_n_score[1]
for mood in lst_mood:
if mood in tweet_text:
if type_mood == 'mood_happy':
mood_happy += 1
elif type_mood == 'mood_sad':
mood_sad += 1
elif type_mood == 'mood_angry':
mood_angry += 1
elif type_mood == 'mood_shocked':
mood_shocked += 1
else:
mood_romantic += 1
break
print('\n----------------')
print 'mood_happy:', mood_happy
print 'mood_sad:', mood_sad
print 'mood_angry:', mood_angry
print 'mood_shocked:', mood_shocked
print 'mood_romantic:', mood_romantic
# 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(twitter_access["consumer_key"], twitter_access["consumer_secret"])
auth.set_access_token(twitter_access["access_token"], twitter_access["access_token_secret"])
# api = API(auth)
twitterStream = Stream(auth, Listener(), timeout=None)
for list_of_mood in [happy, sad, angry, shocked, romantic]:
twitterStream.filter(track=list_of_mood)