我使用Python-Twitter library中的GetStreamFilter
功能遇到了一些麻烦。
我使用过这段代码:
import twitter
import time
consumer_key = 'myConsumerKey'
consumer_secret = 'myConsumerSecret'
access_token = 'myAccessToken'
access_token_secret = 'myAccessTokenSecret'
apiTest = twitter.Api(consumer_key,
consumer_secret,
access_token,
access_token_secret)
#print(apiTest.VerifyCredentials())
while (True):
stream = apiTest.GetStreamFilter(None, ['someWord'])
try:
print(stream.next())
except:
print ("No posts")
time.sleep(3)
我想要做的是获取包含单词" someWord"的新推文,并且每隔三秒(或每次发布一个新的推文)执行这些推文。 / p>
答案 0 :(得分:2)
您只需要在循环外创建一次流。
stream = apiTest.GetStreamFilter(None, ['someWord'])
while True:
for tweet in stream:
print(tweet)
答案 1 :(得分:0)
如何用一个从流中提取内容的循环替换你的while True
?
for tweet in apiTest.GetStreamFilter(track=['someWord']):
print tweet