0
如果您只想查看所选推特ID的推文,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)