Python从tweepy返回流数据

问题描述 投票:0回答:1

我正在构建一个模块source_collect.py,该模块将包含将用于从各种来源收集数据并返回原始数据的模块,可以使用第二个模块进行解析。

我能够将流数据打印或写入json文件,但是我无法通过return函数返回数据。有人对我遗漏的内容有som指针吗?

import tweepy

import twitter_credentials as cred

class my_streaming_object(tweepy.streaming.StreamListener):

    def on_data(self,raw_data):
        try:
            print('......streaming...')
            self.raw_data = raw_data
            return self.raw_data

        except BaseException as e:
            print(f'Code broke :{str(k)}')
            return False

    def on_error(self,status_code):
        if status_code ==420:
            print(f'Twitter limit stop{self.status.code}')
            return False


class twitter_data:

    def stream_tweets_keywords(self, keywords):
        # get twitter access
        authen = tweepy.auth.OAuthHandler(cred.CONSUMER_KEY,cred.CONSUMER_SECRET)
        authen.set_access_token(cred.ACCESS_TOKEN,cred.ACCESS_TOKEN_SECRET)
        test = authen.get_username()
        print(f'You are Authenticated as Twitter user {test}')
        print('....Initialising twitter stream......')
        # stream tweets
        try:
            streamer = my_streaming_object()
            my_stream = tweepy.streaming.Stream(authen, listener=streamer)
            my_stream.filter(track=keywords, is_async=True)
            return
        except BaseException as b:
            print(b)
            return False




if __name__=='__main__':

    run = twitter_data()
    run.stream_tweets_keywords(['Hydrogen','Nikola'])
python python-3.6 tweepy
1个回答
0
投票

通过更改我的逻辑已解决。我在该模块中使用的DB_store模块中创建了一个函数,用于处理对db的流式响应。

逻辑上的改变不是让此模块返回,而是在下一个函数输入中进行选择。我在这个函数中嵌套了下一个函数。

我现在不这样做,如果有什么好的做法,因此,如果任何机构对整体逻辑都有良好的OOP准则,将不胜感激。

© www.soinside.com 2019 - 2024. All rights reserved.