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)