如果您只想查看来自选定推特 ID 的推文,则 Tweepy 中有一个功能。
streaming_api.filter(follow=("501088042","107536557",), track=Q)
不幸的是,它要么不起作用(非常值得怀疑),要么我做错了什么。如果我follow=None
完美地设置脚本功能。当我设置用户 ID 时,它会继续工作,就好像我什么都没做一样。如何过滤我的流以仅使用我设置的 ID follow
?
这是代码:
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)