I'm trying to search twitter for a finite period of time in the past couple days to pull 2-3 keywords, but I can't seem to figure out how to search multiple terms at once.
There are probably other ways to do this, but I've got a short code using tweepy.cursor. But I can't figure out how to search multiple query terms at once with this? (The example I have is wanting to search #superbowl / superbowl / super bowl all at once) I've tried several things, but the code below seems to return the AND logic statement. I have seen any documentation that helps.
Any help appreciated!
import tweepy
from datetime import datetime,timedelta
import csv
access_token = "..."
access_token_secret = "..."
consumer_key = "..."
consumer_secret = "..."
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
csvFile = open('output.csv', 'a')
csvWriter = csv.writer(csvFile)
search_terms = 'superbowl ', 'super bowl ', '#superbowl'
for status in tweepy.Cursor(api.search,
q=search_term,
since='2017-02-16', until='2017-02-17',
count=10,
result_type='recent',
include_entities=True,
monitor_rate_limit=True,
wait_on_rate_limit=True,
lang="en").items():
eastern_time = status.created_at - timedelta(5)
edt_time = eastern_time.strftime('%Y-%m-%d %H:%M')
csvWriter.writerow([status.created_at, status.user.screen_name.encode('utf8'), status.text.encode('utf-8')])
csvFile.close()
You use tweepy in exactly the same manner as with a single keyword, but the query parameter q
should have your multiple keywords. For example, to search for tweets containing either the word "cupcake" or "donut" you pass in the string "cupcake OR donut" as the q
parameter.