Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tweepy.cursor multiple / OR logic function for query terms

Tags:

python

tweepy

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()
like image 859
Kees Avatar asked Feb 22 '17 06:02

Kees


People also ask

How do you search multiple words in Tweepy?

1 Answer. Show activity on this post. 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.

What does Tweepy cursor do?

But if you want tweets in a specific language, you can use tweets = tweepy. Cursor(api.search, q="Giraffe", since = "2020-06-05", until = "2020-06-21",lang="en") this will fetch you tweets posted in English. I hope it helps.

How can I get more than 100 tweets on Tweepy?

If you need more than 100 Tweets, you have to use the paginator method and specify the limit i.e. the total number of Tweets that you want. Replace limit=1000 with the maximum number of tweets you want. Replace the limit=1000 with the maximum number of tweets you want (gist).

Does Tweepy work with v2?

If you want to retweet a Tweet with Tweepy using the Twitter API v2, you will need to make sure that you have your consumer key and consumer secret, along with your access token and access token secret, that are created with Read and Write permissions (similar to the previous example).


1 Answers

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.

like image 193
Lena Avatar answered Oct 19 '22 23:10

Lena