Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8376453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:26:09+00:00 2026-06-09T15:26:09+00:00

I have a file that pulls info from twitter in python and runs the

  • 0

I have a file that pulls info from twitter in python and runs the status messages into my shell. I want to take them from the shell and into a database. I do not know how to do that. I do not have a database made for it either, I will go on the database stack to question that. My code is as follows:

import time
import MySQLdb
import tweepy
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream

# Go to http://dev.twitter.com and create an app.
# The consumer key and secret will be generated for you after
consumer_key=" # Omitted "
consumer_secret=" # Omitted "

# After the step above, you will be redirected to your app's page.
# Create an access token under the the "Your access token" section
access_token=" # Omitted"
access_token_secret=" #Omitted "

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

# If the authentication was successful, you should
# see the name of the account print out
print api.me().name

class StdOutListener(StreamListener):
        """ A listener handles tweets are the received from the stream.
        This is a basic listener that just prints received tweets to stdout.
        """
        def on_data(self, data):
            print data
            return True

        def on_error(self, status):
            print status

if __name__ == '__main__':
        l = StdOutListener()

        stream = Stream(auth, l)    
        stream.filter(track=['search term'])

How do I get the info from the stream into a database? I also want to filter the stream to only allow certain information in. The only information I want to go into the database is the following:

  1. Message Author
  2. Message
  3. Date / Time Stamp
  4. GEO if available
  5. Source ie Tweetdeck, web, mobile yaddda yadda
  6. Was the message RT’d
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:26:11+00:00Added an answer on June 9, 2026 at 3:26 pm

    This question asks for a lot of information, so I am just going to give you an overview of what you need…

    First off, there are examples of this exact approach both on SO and across internet. Here is a paste example from this tutorial

    class StreamListener(tweepy.StreamListener):
    
        status_wrapper = TextWrapper(width=60, initial_indent='    ', 
                                        subsequent_indent='    ')
        conn = mdb.connect('localhost', 'dbUser','dbPass','dbBase')
    
        def on_status(self, status):
            try:
                cursor = self.conn.cursor()
                cursor.execute('INSERT INTO tweets (text, date) VALUES (%s, NOW())' ,(status.text))
                print self.status_wrapper.fill(status.text)
                print '\n %s  %s  via %s\n' % (status.author.screen_name, status.created_at, status.source)
            except Exception, e:
                # Catch any unicode errors while printing to console
                # and just ignore them to avoid breaking application.
                pass
    

    This example uses a different database driver. But you would use the on_status handler to receive new data, and split it up into values. Then you create an sql INSERT to put it into your database.

    Here is an sqlite3 example from this SO question:

    cur.execute("INSERT INTO TWEETS(?, ?, ?, ?)", (status.text, 
                                                   status.author.screen_name, 
                                                   status.created_at, 
                                                   status.source))
    

    Both of these example require that you use your database client to connect, and then get a cursor (which is an object that makes your queries and lets you look through the results). You can check out a tutorial on MySQLdb for a pretty good overview of how to set everything up and make queries.

    If you end up having a more focused problem, then that can be addressed separately.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Python code that pulls info from a sqlite database and then write
I have written some code that pulls info from a plist file and does
I have a file that I want to import into Microsoft Dynamics CRM 2011
I have a little python script that pulls emails from a POP mail address
I have a file that contains this <!-- CordovaVersion --> I want to replace
I have a file that I need to parse into an array but I
My teams ant task pulls in developer specific info from a build.properties file by
I have a script that pulls an image from SQL Server and displays it
I have an @import at the top of my main.css file that pulls in
I have been working on a script that pulls information from a certain website.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.