python - 为什么我从 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'])
解决方案
推荐阅读
- git - 什么时候单个文件被认为对 git 来说太大了?
- python - Python,Shell:如何提取数据并存储在可迭代变量中(特别是在列表中)?
- nginx - 如果重新启动日志记录实例,Nginx 无法将日志发送到远程 syslog
- c++ - vector.size() = 0 和 vector.empty() 之间有区别吗?
- java - 多键串流分组
- google-analytics - 从谷歌网站副本中删除谷歌分析
- java - 对接口的可变对象的 PMD 引用
- python - 我需要在 for 循环中排除列表中的元素,并通过使用 python 搜索排除的元素来更新 xml 文件
- django - if语句中的Django类字段
- scala - 如何避免:“blockingToByteString 是一种阻塞且不安全的操作”