我正在尝试关注example here,但我想要多个听众。如何在没有多线程的情况下创建多个侦听器?
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
#Variables that contains the user credentials to access Twitter API
access_token = "<...>"
access_token_secret = "<...>"
consumer_key = "<...>"
consumer_secret = "<...>"
#This is a basic listener that just prints received tweets to stdout.
class StdOutListener(StreamListener):
def on_data(self, data):
print data
return True
def on_error(self, status):
print status
if __name__ == '__main__':
#This handles Twitter authetification and the connection to Twitter Streaming API
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)
#This line filter Twitter Streams to capture data by the keywords: 'python', 'javascript', 'ruby'
stream.filter(track=['python', 'javascript', 'ruby'])
print "Control never seems to arrive here"
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)
#This line filter Twitter Streams to capture data by the keywords: 'python', 'javascript', 'ruby'
stream.filter(track=['django', 'angularjs', 'rails'])
Stackoverflow需要更多文本 Stackoverflow需要更多文本 Stackoverflow需要更多文本
答案 0 :(得分:0)
我自己也遇到过同样的问题。 我的解决方案是在新的命令行进程中运行脚本的不同实例。如果这样做,则需要将脚本的每个“版本”保存为单独的文件。 对此可能有一个更好的解决方案,但它是我现在最好的解决方案。
希望这至少有一点帮助。
答案 1 :(得分:0)
正如streaming API documentation的 function getXmlDom(content) {
var xmlDom;
try {
xmlDom = new ActiveXObject('Msxml2.DOMDocument.6.0');
xmlDom.loadXML(content);
}
catch (e) {
try {
xmlDom = new ActiveXObject('Msxml2.DOMDocument.3.0');
xmlDom.loadXML(content);
}
catch (e2) {
xmlDom = (new DOMParser()).parseFromString(content, 'application/xml');
}
}
return xmlDom;
}
部分所述,Twitter只允许您连接到流终端。连接尝试次数过多可能导致他们禁止您的IP。