【问题标题】:Get tweets that have a specific keyword and other optional keywords in python在 python 中获取具有特定关键字和其他可选关键字的推文
【发布时间】:2016-03-14 12:44:22
【问题描述】:

我正在尝试过滤来自 twitter 的实时推文,我有这个列表关键字=[“Alex”、“love”、“hate”、“hungry”、“happy”] 我想获得包含“ Alex”和给定列表中的至少一个关键字。当我运行它时,我的代码在下面,它会跟踪包含列表中任何单词的推文。再次记住,我希望“Alex”作为主要跟踪关键字,tweet 应该有“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 = "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"])

【问题讨论】:

    标签: python twitter


    【解决方案1】:

    假设您将推文存储在名为 tweet 的变量中

    keywords = ['love', 'hate', 'hungry', 'happy']
    if "Alex" in tweet:
        if any(keyword in tweet for keyword in keywords):
            # get the tweet
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多