0

我正在使用 PYSpark 实时流式传输推文。

我想检索文本、位置、用户名。目前,我只接收推文。无论如何也可以得到位置。

lines = ssc.socketTextStream("localhost", 5550)

我正在使用这行代码来获取推文。

4

1 回答 1

0

我刚刚找到了答案..我们需要更新推特监听器..

def on_data(self, data):
    try:
        msg = json.loads(data)
        if ('retweeted_status' in msg):
            if ('extended_tweet' in msg['retweeted_status']):
                print(msg['retweeted_status']['extended_tweet']['full_text'])
                print(" | The Location is " + str(msg['user']['location']) )
                self.client_socket.send((str(msg['retweeted_status']['extended_tweet']['full_text']) + "\n").encode('utf-8'))
        elif ('extended_status' in msg):
            print(msg['extended_status']['full_text'])
            print(" | The Location is " + str(msg['user']['location']) )
            self.client_socket.send((str(msg['extended_status']['full_text']) + "\n").encode('utf-8'))
        else:
            print(msg['text'])
            print(" | The Location is " + str(msg['user']['location']) )
            self.client_socket.send((str(msg['text']) + "\n").encode('utf-8'))
    except BaseException as e:
        print("Error on_data: %s" % str(e))

    return True 
于 2019-08-08T18:30:10.947 回答