我为什么会遇到这个错误?以下是我如何调用流式 API。
import tweepy
import sys
creds = json.loads(open('credential.json').read())
tw_consumer_key = creds['tw_consumer_key']
tw_consumer_secret = creds['tw_consumer_secret']
tw_access_token = creds['tw_access_token']
tw_access_token_secret = creds['tw_access_token_secret']
try:
auth = tweepy.OAuthHandler(tw_consumer_key, tw_consumer_secret)
auth.set_access_token(tw_access_token, tw_access_token_secret)
api = tweepy.API(auth)
except Exception:
service = None
api = None
# Query terms
Q = "Better"
class CustomStreamListener(tweepy.StreamListener):
def on_status(self, status):
try:
print "%s\n%s\n%s\n%s\n\n" % (status.text,
status.author.screen_name,
status.created_at,
status.source,)
except Exception, e:
print >> sys.stderr, 'Encountered Exception:', e
pass
def on_error(self, status_code):
print >> sys.stderr, 'Encountered error with status code:', status_code
return True # Don't kill the stream
def on_timeout(self):
print >> sys.stderr, 'Timeout...'
return True # Don't kill the stream
class LiveStream(webapp2.RequestHandler):
def get(self):
streaming_api = tweepy.streaming.Stream(auth, CustomStreamListener(), timeout=60)
self.response.out.write(streaming_api.filter(track=Q))
可能是因为GAE不允许使用Socket,我不确定如何应用查询术语来获取特定的过滤流推文。我编写这部分代码的目的是获取带有指定关键字的实时流。如果有任何替代方法,请指导。