我正在尝试用以下代码流式传输twitter数据:import tweepy from tweepy import oauthhandler#to authenticate tweepy api from tweepy import stream from tweepy.streaming import streamlistener import socket import json
# Twitter developer Credentials to connect to twitter account
access_token = "access token"
access_secret = "access secret"
consumer_key = "consumer key"
consumer_secret = "consumer secret"
class TweetsListener(StreamListener):
# initialized the constructor
def __init__(self, csocket):
self.client_socket = csocket
def on_data(self, data):
try:
# read the Twitter data which comes as a JSON format
msg = json.loads(data)
# the 'text' in the JSON file contains the actual tweet.
print(msg['text'].encode('utf-8'))
# the actual tweet data is sent to the client socket
self.client_socket.send(msg['text'].encode('utf-8'))
print("4")
return True
except BaseException as e:
# Error handling
print("Ahh! Look what is wrong : %s" % str(e))
return True
def on_error(self, status):
print(status)
return True
def sendData(c_socket):
# authentication
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
# twitter_stream will get the actual live tweet data
twitter_stream = Stream(auth, TweetsListener(c_socket))
# filter the tweet feeds related to "corona"
twitter_stream.filter(track=['marvel'])
# in case you want to pass multiple criteria
# twitter_stream.filter(track=['DataScience','python','Iot'])
# create a socket object
s = socket.socket()
# Get local machine name : host and port
host = "127.0.0.1"
port = 7777
# Bind to the port
s.bind((host, port))
print("Listening on port: %s" % str(port))
# Wait and Establish the connection with client.
s.listen(5)
c, addr = s.accept()
print("Received request from: " + str(addr))
sendData(c)
我知道print“4”语句没有打印是因为上面的语句,而向socket发送tweet数据是导致错误的原因,我无法确定原因。有人能帮我吗?提前谢谢
暂无答案!
目前还没有任何答案,快来回答吧!