我正在尝试过滤来自Twitter的实时推文,我有这个列表关键字= [" Alex"," love"," hate",& #34;饥饿","快乐"]我想收到有#" Alex"以及给定列表中的至少一个关键字。当我运行它时,我的代码在下面,它跟踪包含列表中任何单词的推文。再次记得我想要" Alex"作为主要的跟踪关键字,推文应该有" Alex"至少有一个词是"爱",#34;讨厌",#34;饥饿","快乐"。
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import json
# consumer key, consumer secret, access token, access secret.
ckey = "xxxxxxxxxxxxxxx"
csecret = "xxxxxxxxxxxxxxxxxx"
atoken = "xxxxxxxxxxxxxxxxxx"
asecret = "xxxxxxxxxxxxxxxxxxx"
class listener(StreamListener):
def on_data(self, data):
all_data = json.loads(data)
tweet = all_data["text"]
username = all_data["user"]["screen_name"]
out = open('out1.txt', 'a')
out.write(tweet.encode('utf-8'))
out.write('\n')
out.close()
print username, " :: ", tweet
return True
def on_error(self, status):
print status
auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
keywords = ["Alex", "love","hate","hungry","happy"]
twitterStream = Stream(auth, listener())
twitterStream.filter(track=keywords, languages=["en"])
答案 0 :(得分:2)
假设您将推文存储在名为tweet
keywords = ['love', 'hate', 'hungry', 'happy']
if "Alex" in tweet:
if any(keyword in tweet for keyword in keywords):
# get the tweet