Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中,如何使用主关键字和可选关键字获取特定关键字_Python_Twitter - Fatal编程技术网

在python中,如何使用主关键字和可选关键字获取特定关键字

在python中,如何使用主关键字和可选关键字获取特定关键字,python,twitter,Python,Twitter,我正在尝试过滤twitter上的实时推文,我有这个列表关键字=[“Alex”,“love”,“hate”,“Hunger”,“happy”]我想从给定列表中获得至少有“Alex”和一个或多个关键字的推文。我的代码在下面,当我运行它时,它会跟踪包含列表中任何单词的tweet。再次记住,我想把“Alex”作为推特的主要关键词,它应该有“Alex”,其余的“love”、“hate”、“hungry”、“happy”是可选的关键词 from tweepy import Stream from tweep

我正在尝试过滤twitter上的实时推文,我有这个列表关键字=[“Alex”,“love”,“hate”,“Hunger”,“happy”]我想从给定列表中获得至少有“Alex”和一个或多个关键字的推文。我的代码在下面,当我运行它时,它会跟踪包含列表中任何单词的tweet。再次记住,我想把“Alex”作为推特的主要关键词,它应该有“Alex”,其余的“love”、“hate”、“hungry”、“happy”是可选的关键词

from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import json

# consumer key, consumer secret, access token, access secret.
ckey = "aaaaaaaaaaaaa"
csecret = "aaaaaaaaaaaaa"
atoken = "aaaaaaaaaaaaa"
asecret = "aaaaaaaaaaaaa"

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"])

你的意思是必须有亚历克斯和其他的一个选择吗?如果是这样的话,这可能是最简单的方法。结果=twitterStream.filter(track=['Alex'],languages=[“en”])。filter(track=keywords,languages=[“en”])@Obj3ctiv3_c88您更正了,但仍然只显示Alex的推文。即使在从主列表中删除“Alex”之后,它也没有显示任何其他关键字,因为我们直接在cod中给出它,并且运行您的代码。我仍然收到的推文只有Alex的推文。您的意思是与Alex的推文,但没有显示其他关键字吗?@Obj3ctiv3_c88完全正确!