我希望能够跟踪包含某些单词的推文,而不是其他单词。例如,如果我的过滤器是:“taco”AND(“鸡”或“牛肉”)。
它应该返回这些推文:
-I am eating a chicken taco.
-I am eating a beef taco.
它不应该返回这些推文:
-I am eating a taco.
-I am eating a pork taco.
这是我目前正在运行的代码:
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import time
import json
# authentication data- get this info from twitter after you create your application
ckey = '...' # consumer key, AKA API key
csecret = '...' # consumer secret, AKA API secret
atoken = '...' # access token
asecret = '...' # access secret
# define listener class
class listener(StreamListener):
def on_data(self, data):
try:
print data # write the whole tweet to terminal
return True
except BaseException, e:
print 'failed on data, ', str(e) # if there is an error, show what it is
time.sleep(5) # one error could be that you're rate-limited; this will cause the script to pause for 5 seconds
def on_error(self, status):
print status
# authenticate yourself
auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
twitterStream = Stream(auth, listener())
twitterStream.filter(track=["taco"]) # track what you want to search for!
代码的最后一行是我正在努力的部分;如果我使用:
twitterStream.filter(track=["taco","chicken","beef"])
它将返回包含三个单词中的任何一个的所有推文。我尝试过的其他事情,例如:
twitterStream.filter(track=(["taco"&&("chicken","beef")])
返回语法错误。
我对Python和Tweepy都很陌生。都 这个 和 这个 看起来像是类似的查询,但它们与同时跟踪多个术语有关,而不是跟踪包含术语的推文子集。我一直无法找到任何东西 tweepy文档。
我知道另一个选择是跟踪所有包含“taco”的推文,然后通过“chicken”或“beef”过滤到我的数据库中,但是我担心如果我进行一般搜索然后再遇到1%的流速限制在Python中过滤掉它,所以我更喜欢从Twitter首先流式传输我想要的术语。
提前致谢-
山姆