我想獲得下面的代碼,以排除包含列表中的限制詞的任何推文。做這個的最好方式是什麼?Tweepy - 限制某些推文
此代碼還返回只有最後一次我打破流的鳴叫。有沒有辦法將所有適用的推文打印成CSV?
import sys
import tweepy
import csv
#pass security information to variables
consumer_key = ''
consumer_secret = ''
access_key = ''
access_secret = ''
#use variables to access twitter
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
#create an object called 'customStreamListener'
class CustomStreamListener(tweepy.StreamListener):
def on_status(self, status):
print (status.author.screen_name, status.created_at, status.text)
# Writing status data
with open('OutputStreaming.csv', 'w') as f:
writer = csv.writer(f)
writer.writerow([status.author.screen_name, status.created_at, status.text])
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
# Writing csv titles
with open('OutputStreaming.csv', 'w') as f:
writer = csv.writer(f)
writer.writerow(['Author', 'Date', 'Text'])
streamingAPI = tweepy.streaming.Stream(auth, CustomStreamListener())
streamingAPI.filter(track=['Hasbro', 'Mattel', 'Lego'])
你解決這個問題? – Giordano
還在黑客嗎? – JoshGumby87
我覺得我在這裏很好。我得到它運行,但過濾也許我需要進一步研究使用不同的包。 – JoshGumby87