Search term intersection and union using Python Tweepy
Asked Answered
C

1

2

I would like to get the tweets that either contain 'love' and/or '#hate' using Python Tweepy. But using my current code as below, it only returns the first term (i.e. 'love'). I have been trying for days to debug and read the Tweepy/Twitter documentation to no avail. Please advice.

import tweepy
import time

ckey = ""
csecret = ""
atoken = ""
asecret = ""

OAUTH_KEYS = {'consumer_key':ckey, 'consumer_secret':csecret,
    'access_token_key':atoken, 'access_token_secret':asecret}
auth = tweepy.OAuthHandler(OAUTH_KEYS['consumer_key'], OAUTH_KEYS['consumer_secret'])
api = tweepy.API(auth)

for tweet in tweepy.Cursor(api.search, q=('love' or '#hate'), since='2014-09-15', until='2014-09-16').items(500): 
    try:

        print "Tweet created:", tweet.created_at
        print "Tweet:", tweet.text.encode('utf8')

        counter += 1

    except IOError:
        time.sleep(60)
        continue
Christopherchristopherso answered 16/9, 2014 at 20:50 Comment(0)
C
5

From looking at the twitter API documentation, the 'or' should be capitalized and should be inside the single quotes:

q = ('love OR #hate')

https://dev.twitter.com/rest/public/search

Chaffin answered 16/9, 2014 at 22:45 Comment(1)
I did look at the Twitter documentation, and I have tried q = ("love" OR "#hate") and not worked and thought it's not relevant. It turns out it needs to be in one string argument, as you suggested. Thanks.Christopherchristopherso

© 2022 - 2024 — McMap. All rights reserved.