Python 为什么我从Twitter提取关键词的代码没有打印任何内容?
我试图构建一个关键字提取器,通过TwitterStream从tweet中提取关键字,但当我运行此代码时,什么都没有发生,没有错误,也没有输出,它只是一直在运行Python 为什么我从Twitter提取关键词的代码没有打印任何内容?,python,json,nlp,rake,twitter-streaming-api,Python,Json,Nlp,Rake,Twitter Streaming Api,我试图构建一个关键字提取器,通过TwitterStream从tweet中提取关键字,但当我运行此代码时,什么都没有发生,没有错误,也没有输出,它只是一直在运行 from tweepy import Stream from tweepy import OAuthHandler from tweepy.streaming import StreamListener import json from rake_nltk import Rake #consumer key, consumer sec
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'])
通过调试器或仅仅通过添加一些打印,您应该能够知道程序在做什么,以及它是否在某个地方卡住。你试过了吗?是的,我试过了,它不做代码中的“真”:部分,它总是用于“例外”:情况