Tweepy过滤器无法正常工作

时间:2012-02-29 21:34:23

标签: python filter tweepy

如果您只想查看所选推特内容的推文,那么Tweepy中就有一个功能。

streaming_api.filter(follow=("501088042","107536557",), track=Q)
不幸的是,它要么不起作用(非常怀疑),要么我做错了什么。如果我设置follow=None脚本功能完美。当我设置用户ID时,它继续工作,好像我什么都没改变。如何过滤我的流只使用我在follow中设置的ID?

这是代码:

import sys
import tweepy
import webbrowser
import MySQLdb

Q = sys.argv[1:]

db = MySQLdb.connect("localhost","user","password","db" )

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

cur = db.cursor()

class CustomStreamListener(tweepy.StreamListener):

    def on_status(self, status):

        try:
            print "%s\t%s\t%s\t%s" % (status.text, 
                                      status.author.screen_name, 
                                      status.created_at, 
                                      status.source,)

            cur.execute("INSERT INTO tweets VALUES (%s, %s, %s, %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

streaming_api = tweepy.streaming.Stream(auth, CustomStreamListener(), timeout=60)

print >> sys.stderr, 'Filtering the public timeline for "%s"' % (' '.join(sys.argv[1:]),)

streaming_api.filter(follow=("501088042","107536557",), track=Q)

1 个答案:

答案 0 :(得分:0)

修正了它!

streaming_api.filter(follow=['501088042'], track=Q)
相关问题