首页 > 解决方案 > 为什么我从 Twitter 中提取关键字的代码没有打印任何内容?

问题描述

我正在尝试构建一个关键字提取器,它通过 TwitterStream 从推文中提取关键字,但是当我运行此代码时没有任何反应,没有错误,也没有输出,它只是继续运行。

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


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

r = Rake() # Uses stopwords for english from NLTK, and all puntuation characters.


class listener(StreamListener):

    def on_data(self, data):
        try:

            all_data = json.loads(data)

            tweet = all_data["text"]
            r.extract_keywords_from_text(tweet)
            r.get_ranked_phrases()
            return True
        except:
            return True

    def on_error(self, status):
        print (status)

auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)

twitterStream = Stream(auth, listener())
twitterStream.filter(track=['news'])

标签: pythonjsonnlpraketwitter-streaming-api

解决方案


推荐阅读