我希望跟踪包含特定词语的推文,而不是其他的。例如,如果我的过滤器是:“墨西哥卷”和(“鸡肉”或“牛肉”)。
它应该返回这些推文:
-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"])它将返回包含这三个单词中任何一个的所有tweet。我尝试过的其他事情,比如:
twitterStream.filter(track=(["taco"&&("chicken","beef")])返回语法错误。
我对Python和Tweepy都很陌生。这和这看起来都是类似的查询,但它们与同时跟踪多个术语有关,而不是跟踪包含一个术语的tweet子集。我还没有在tweepy文档上找到任何东西。
我知道另一种选择是跟踪包含"taco“的所有tweet,然后用”鸡肉“或”牛肉“过滤到我的数据库中,但我担心如果我做一个普通搜索,然后在Python中过滤掉它,我担心会遇到1%的流速率限制,所以我更喜欢首先从Twitter上流出我想要的内容。
提前谢谢-
相同的
发布于 2014-03-13 19:31:25
Twitter不允许你在关键字匹配方面非常精确。但是,航迹参数文档声明关键字中的空格与逻辑和逻辑相等。你指定的所有条款都是一起的。
因此,要实现"taco" AND ("chicken" OR "beef")示例,可以尝试参数taco chicken,taco beef。这将匹配包含单词taco和chicken,或taco和beef的tweet。然而,这并不是一个完美的解决方案,因为包含taco、chicken和beef的tweet也将被匹配。
https://stackoverflow.com/questions/22339009
复制相似问题