Search code examples
pythontweepy

How to filter keyword search in tweepy


I need to create a program with tweepy for a homework. Im not a programmer. I would like the program to search for menacing tweets toward for example Justin Trudeau. And then send me an email when it spot one. To determine if a tweet is menacing or not, the tweet would have to contain, for example, the keyword "trudeau" and one of the following "bomb" or "kill". Once i get this to work, I'll refine the keyword filter.

So i have tried this:

api = tweepy.API(auth)

searchterm1 = "trudeau"
searchterm2 = "bomb" or "kill"


search = tweepy.Cursor(api.search, 
                       q= searchterm1 and searchterm2
                       lang="en",                    
                       result_type="recent").items(10)

for item in search:
    print (item.text)

But it only shows me tweets with the last keyword, not one of them like it should with the or function, no? I want to show only tweets that contain the word "trudeau" and one of the keyword in searchterm2

Thanks for your help


Solution

  • You're gonna need this, where you'll find that your query string should be:

    q = 'trudeau bomb OR kill'
    

    From your example you can get to that query string like this:

    searchterm1 = 'trudeau'
    searchterm2 = 'bomb OR kill'
    q = ' '.join([searchterm1, searchterm2])