我试着做实时推文分类。为此,我使用了tweepy流类。我也想
问题是我的系统一直在获取并保存它。它不会移动到下一步。我该怎么办?
流代码
import tweepy
class MyStreamListener(tweepy.StreamListener):
def on_status(self, status):
if not status.text.startswith('RT'):
if 'https://t.co' not in status.text:
text=status.text
with open("Output.txt", "w",encoding='utf-8') as text_file:
text_file.write(text)
# Initializing the tokens
def run_stream():
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth,wait_on_rate_limit=True)
myStreamListener = MyStreamListener()
myStream = tweepy.Stream(auth = api.auth, listener=myStreamListener,tweet_mode= 'extended')
myStream.filter(track=['corona'],languages=["en"],encoding='utf-8')Classification
def classify():
f = open("Output.txt", "r",encoding="utf-8")
return f.read(),clf.predict_proba(feat.transform([f.read()]))运行函数
run_stream()
classify()我面临的问题是,系统继续运行第一个函数,而不是移动到下一个分类函数。
发布于 2020-05-16 04:39:07
来自to.html
除非连接关闭,否则流不会终止,从而阻塞线程。Tweepy在过滤器上提供了一个方便的is_async参数,因此流将在一个新线程上运行。例如 myStream.filter(track='python',is_async=True)
如果这样做,我怀疑您将需要仔细管理对Output.txt的读和写访问,否则,当文件包含部分写入时,您可能会读取它。最好是使用一种专门构建的、线程安全的队列机制,比如Python队列。
https://stackoverflow.com/questions/61831850
复制相似问题