Python 使用tweepy的流式api仅返回最后一条tweet,而不是立即返回最后一条tweet
我不仅是python新手,而且是编程新手,所以我非常感谢您的帮助 我正在尝试使用Tweepy过滤检测twitter流API中的所有推文 我已经通过用户id进行了过滤,并确认推文正在实时收集 然而,似乎只有最后一条推文被实时收集,而不是最新的推文 你们能帮忙吗Python 使用tweepy的流式api仅返回最后一条tweet,而不是立即返回最后一条tweet,python,twitter,streaming,tweepy,Python,Twitter,Streaming,Tweepy,我不仅是python新手,而且是编程新手,所以我非常感谢您的帮助 我正在尝试使用Tweepy过滤检测twitter流API中的所有推文 我已经通过用户id进行了过滤,并确认推文正在实时收集 然而,似乎只有最后一条推文被实时收集,而不是最新的推文 你们能帮忙吗 import tweepy import webbrowser import time import sys consumer_key = 'xyz' consumer_secret = 'zyx' ## Getting access
import tweepy
import webbrowser
import time
import sys
consumer_key = 'xyz'
consumer_secret = 'zyx'
## Getting access key and secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth_url = auth.get_authorization_url()
print 'From your browser, please click AUTHORIZE APP and then copy the unique PIN: '
webbrowser.open(auth_url)
verifier = raw_input('PIN: ').strip()
auth.get_access_token(verifier)
access_key = auth.access_token.key
access_secret = auth.access_token.secret
## Authorizing account privileges
auth.set_access_token(access_key, access_secret)
## Get the local time
localtime = time.asctime( time.localtime(time.time()) )
## Status changes
api = tweepy.API(auth)
api.update_status('It worked - Current time is %s' % localtime)
print 'It worked - now go check your status!'
## Filtering the firehose
user = []
print 'Follow tweets from which user ID?'
handle = raw_input(">")
user.append(handle)
keywords = []
print 'What keywords do you want to track? Separate with commas.'
key = raw_input(">")
keywords.append(key)
class CustomStreamListener(tweepy.StreamListener):
def on_status(self, status):
# We'll simply print some values in a tab-delimited format
# suitable for capturing to a flat file but you could opt
# store them elsewhere, retweet select statuses, etc.
try:
print "%s\t%s\t%s\t%s" % (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
# Create a streaming API and set a timeout value of ??? seconds.
streaming_api = tweepy.streaming.Stream(auth, CustomStreamListener(), timeout=None)
# Optionally filter the statuses you want to track by providing a list
# of users to "follow".
print >> sys.stderr, "Filtering public timeline for %s" % keywords
streaming_api.filter(follow=handle, track=keywords)
这是一种输出缓冲的情况。使用
-u
(无缓冲)运行python以防止发生这种情况
或者,可以通过在print语句之后执行sys.stdout.flush()
来强制刷新缓冲区
有关更多想法,请参阅。我也有同样的问题。在我的例子中,答案并不像运行python unbuffered那么简单,我认为它也不能解决原始海报的问题。问题实际上在于tweepy包的代码中有一个名为streaming.py的文件和函数_read_loop(),我认为需要对其进行更新,以反映twitter从流api输出数据的格式的变化 我的解决方案是从github下载tweepy的最新代码,特别是streaming.py文件。您可以在该文件的提交历史记录中查看最近所做的更改,以尝试解决此问题 我查看了tweepy类的详细信息,发现streaming.py类在json tweet流中的读取方式存在问题。我认为这与twitter更新他们的流式api有关,以包含传入状态的位数。长话短说,我在streaming.py中替换了这个函数来解决这个问题
def _read_loop(self, resp):
while self.running and not resp.isclosed():
# Note: keep-alive newlines might be inserted before each length value.
# read until we get a digit...
c = '\n'
while c == '\n' and self.running and not resp.isclosed():
c = resp.read(1)
delimited_string = c
# read rest of delimiter length..
d = ''
while d != '\n' and self.running and not resp.isclosed():
d = resp.read(1)
delimited_string += d
try:
int_to_read = int(delimited_string)
next_status_obj = resp.read( int_to_read )
# print 'status_object = %s' % next_status_obj
self._data(next_status_obj)
except ValueError:
pass
if resp.isclosed():
self.on_closed(resp)
这个解决方案还需要学习如何下载tweepy包的源代码,修改它,然后将修改后的库安装到python中。这是通过进入顶级tweepy目录并根据您的系统键入sudo setup.py install之类的内容来完成的
我还就这个软件包向github上的程序员发表了评论,让他们知道发生了什么。谢谢!我知道这是件小事。我已经完成了他们的回购协议,并将此修复,只是等待一个请求。暂时,你可以在这里找到固定版本:@robbrit-谢谢!我真的很感激。拉动完成了吗?