Twitter Search (Python)

Building on yesterday’s fun with Twython, today I looked to expand my Python program (temporary commentating out the option to post a tweet) with an option to search Tweets for a keyword.

Twython brings back a lot of data in its results so I have used the JSON knowledge I got making my NASA NEO Python program to limit what data my Twitter Search program returned.

tweet_search


# geektechstuff
# Twitter Application

from twython import Twython
#Twython is a Twitter Python library

#imports the Twitter API keys

from auth import (
    consumer_key,
    consumer_secret,
    access_token,
    access_token_secret
)

twitter = Twython(
    consumer_key,
    consumer_secret,
    access_token,
    access_token_secret
)

# This posts Tweets
# message = "geektechstuff.com Test Tweet aka /'Hello World!/'"
# twitter.update_status(status=message)
# print("Tweeted: %s" % message)

# This searches Tweets

print('GeekTechStuff Twitter Search Python Program')
print('')
search_term = input('What word should I look for? ')

results = twitter.cursor(twitter.search, q=search_term)

print('')
print('Searching Twitter...')
print('')

for result in results:

    name = result['user']
    screen_name = name['screen_name']

    creation_date = result['created_at']

    tweet_txt = result['text']


    print('Twitter User:', screen_name)
    print('Posted:')
    print(tweet_txt)
    print('at:')
    print(creation_date)
    print('')


If you want to read up on Twython, check out https://twython.readthedocs.io/en/latest/

2 thoughts on “Twitter Search (Python)

  1. Nice post. I will try out something similar in near future so it’s useful to see a similar project. 😀

    One recommendation: You can just add \n to start and/or end in stead of typing print(‘’)
    I would also use .format() in stead of %, but I guess is my personal choice

    Thanks again for the post! 🎉

    Liked by 1 person

Comments are closed.