理解这段python代码到底做什么?

2024-10-03 23:17:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我最近开始学习python,我正在玩tweepy。现在我有点搞不清这段代码是做什么的,尤其是类部分,以及传递给类的是什么。你知道吗

对python类和对象做了一些研究,但仍然没有得到多少成果。你知道吗

class PrintListener(tweepy.StreamListener):
    def on_data(self, data):
        tweet = json.loads(data)


        print('@%s: %s' % (tweet['user']['screen_name'], tweet['text'].encode('ascii', 'ignore')))

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

Tags: 对象代码selfjsondataondefstatus
1条回答
网友
1楼 · 发布于 2024-10-03 23:17:58

Tweepy website

In Tweepy, an instance of tweepy.Stream establishes a streaming session and routes messages to StreamListener instance. The on_data method of a stream listener receives all messages and calls functions according to the message type. The default StreamListener can classify most common twitter messages and routes them to appropriately named methods, but these methods are only stubs.

因此基本上,一旦成功,on_data方法将以JSON文件的形式接收消息,并打印tweet以及关于作者的信息。你知道吗

相关问题 更多 >