python - Logical Operators in Tweepy Filter -
i'm hoping track tweets contain set of words, not others. example, if filter is: "taco" , ("chicken" or "beef").
it should return these tweets:
-i eating chicken taco. -i eating beef taco. it should not return these tweets:
-i eating taco. -i eating pork taco. here code i'm running:
from tweepy import stream tweepy import oauthhandler tweepy.streaming import streamlistener import time import json # authentication data- info twitter after create 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 whole tweet terminal return true except baseexception, e: print 'failed on data, ', str(e) # if there error, show time.sleep(5) # 1 error you're rate-limited; cause script pause 5 seconds def on_error(self, status): print status # authenticate auth = oauthhandler(ckey, csecret) auth.set_access_token(atoken, asecret) twitterstream = stream(auth, listener()) twitterstream.filter(track=["taco"]) # track want search for! the last line of code part i'm struggling with; if use:
twitterstream.filter(track=["taco","chicken","beef"]) it return tweets containing of 3 words. other things i've tried, such as:
twitterstream.filter(track=(["taco"&&("chicken","beef")]) return syntax error.
i'm new both python , tweepy. both this , this seem similar queries, related tracking multiple terms simultaneously, rather tracking subset of tweets containing term. haven't been able find in tweepy documentation.
i know option tracking tweets containing "taco" filtering "chicken" or "beef" database, i'm worried running against 1% streaming rate limit if general search , filter down within python, i'd prefer streaming terms want in first place twitter.
thanks in advance-
sam
twitter not allow precise in how keywords matched. however, track parameter documentation states spaces within keyword equivelent logicals ands. of terms specify or'd together.
so, achieve "taco" , ("chicken" or "beef") example, try parameters [taco chicken, taco beef]. match tweets containing words taco , chicken, or taco , beef. however, isn't perfect solution, tweet containing taco, chicken, , beef matched.
Comments
Post a Comment