Skip to content Skip to sidebar Skip to footer

How To Download Twitter Feed

I am quite the novice when it comes to coding. How do I modify this sample code to download tweets using Python? def get_tweets(api, input_query): for tweet in tweepy.Cursor(a

Solution 1:

The NameError is saying the python script isn't receiving command line arguments, sys.argv[1] being the "subject". Replace "Enter subject here" with the subject you wish to search.

In this example, springbreak is sys.argv[1]:

C:\> python print_tweets.py springbreak

should return and print out tweet texts containing your "subject".

You may also need to change:

if __name__ == "__version__":

to

if __name__ == "__main__":

as __main__ is the entry-point to the script.

The entire script:

#!/usr/bin/env python

import sys
import tweepy

def get_tweets(api, input_query):
    for tweet in tweepy.Cursor(api.search, q=input_query, lang="en").items():
        yield tweet

if __name__ == "__main__":
    input_query = sys.argv[1]

    access_token = "REPLACE_YOUR_KEY_HERE"
    access_token_secret = "REPLACE_YOUR_KEY_HERE"
    consumer_key = "REPLACE_YOUR_KEY_HERE"
    consumer_secret = "REPLACE_YOUR_KEY_HERE"
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = tweepy.API(auth)
    tweets = get_tweets(api, input_query)
    for tweet in tweets:
        print(tweet.text)

Post a Comment for "How To Download Twitter Feed"