Python Twitter流式API返回挂起
今年春天,我构建了一个Python应用程序,旨在持续监控公共流并收集关于特定主题的推文。代码运行得很好,但现在当我想继续它时,如果流几乎不经常收到tweet,我会不断得到{hangup:True}响应。我还没有弄清楚发生了什么变化,我应该怎么做来解决这个问题 我正在使用sixohsix的Twitter库: 下面是我监视流的代码:Python Twitter流式API返回挂起,python,twitter,Python,Twitter,今年春天,我构建了一个Python应用程序,旨在持续监控公共流并收集关于特定主题的推文。代码运行得很好,但现在当我想继续它时,如果流几乎不经常收到tweet,我会不断得到{hangup:True}响应。我还没有弄清楚发生了什么变化,我应该怎么做来解决这个问题 我正在使用sixohsix的Twitter库: 下面是我监视流的代码: q = 'huuhkajat' # Comma-separated list of terms print sys.stderr, 'Filtering the pub
q = 'huuhkajat' # Comma-separated list of terms
print sys.stderr, 'Filtering the public timeline for track="%s"' % (q,)
api = authTwitter.getApi()
mongodb = DatabaseHandler()
analyze = Analysis(mongodb.getDictionary())
# Reference the self.auth parameter
twitter_stream = twitter.TwitterStream(auth=api.auth) # See https://dev.twitter.com/docs/streaming-apis
stream = twitter_stream.statuses.filter(track=q)
try:
i = 0
for tweet in stream:
print tweet
if 'lang' in tweet.keys() and tweet['lang'] == 'fi':
print tweet['text'] + " " + tweet['lang']
print tweet['place']
analyze.analyseTweet(tweet)
if i % 1 == 0:
print
analyze.printStatistics()
entry = {'positive' : analyze.getPositivePercent(), 'negative' : analyze.getNegativePercent(), 'neutral' : analyze.getNeutralPercent(), '_id' : 'sentimentPercentages', 'totalCount' : analyze.getCount(), 'latestTweet' : tweet, 'query' : q}
mongodb.saveToDb(entry, mongodb.statisticCollection)
mongodb.storeToDb(tweet, q) #tallentaa collectioniin jonka nimi on hakusana
print
i += 1
except twitter.TwitterHTTPError, e:
f = open('streamErrors.log','w')
f.write(e.message+'\n')
f.close()
print "ERROR " + e.message
感谢您的帮助:)如果您的分析耗时太长,Twitter将挂断您的电话。 也许您的程序不够快,无法实时处理采样器提要? 您的程序在MongoDB中进行分析和存储需要多长时间
考虑只转储一些记录进行分析,并从磁盘而不是实时处理它们。最后,问题是由稍微陈旧的twitter库引起的。通过pip更新它,脚本看起来像过去一样工作 事实上,这不是问题所在。问题似乎在于,我没有足够快地获得连接所需的tweet,因为我正在寻找关于相对罕见主题的tweet(因为它们是芬兰语的)。所以我什么都不分析。我想,即使没有持续不断的推特,我应该如何保持连接的方式也发生了变化。