Twitter Search (Python)

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 responses to “Twitter Search (Python)”

  1. Mr. Krumins Avatar

    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

  2. Twitter Auto Reply To Tweets (Python) – Geek Tech Stuff Avatar

    […] so my last post dealt with searching with Twython and this lead to me to thinking; what about replying to tweets […]

    Like

Welcome to GeekTechStuff

my home away from home and where I will be sharing my adventures in the world of technology and all things geek.

The technology subjects have varied over the years from Python code to handle ciphers and Pig Latin, to IoT sensors in Azure and Python handling Bluetooth, to Ansible and Terraform and material around DevOps.

Let’s connect