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

if __name__ == "__version__":
    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)

I am trying to download data from Twitter using the command prompt. I have entered my keys (I just recreated them all), saved the script as "print_tweets" and am entering "python print_tweets.py subject" into the command prompt but nothing is happening, no error message or anything.

I thought the problem might have to do with the path environment, but I created another program that prints out "hello world" and this executed without issue using the command prompt.

Can anyone see any obvious errors with my code above? Does this work for you? I've even tried changing "version" to "main" but this gives me an error message:

if name == "version":

Aoitori
  • 67
  • 2
  • 8
  • Didn't you ask this same question yesterday? http://stackoverflow.com/questions/43219596/how-to-download-twitter-feed. I mentioned you need to include the underscores `__` -->`"__main__"`, not `"main"`, like this: `if __name__ == "__main__":` – chickity china chinese chicken Apr 05 '17 at 19:09
  • I think I'm almost there but it won't execute. when I enter "__main__" I get this error NameError Traceback (most recent call last) in () 4 5 if __name__ == "__main__": ----> 6 input_query = sys.argv[1] 7 8 access_token = "X...." NameError: name 'sys' is not defined – Aoitori Apr 05 '17 at 19:16
  • put `import sys` as the *first* line at the top of your script. – chickity china chinese chicken Apr 05 '17 at 19:18
  • if you are running this all in an ipython interpreter, you can replace `input_query = sys.argv[1]` with `input_query = "the topic you want to search"` – chickity china chinese chicken Apr 05 '17 at 19:21

1 Answers1

1

It seems you are running the script in an ipython interpreter, which won't be receiving any command line arguments. Try this:

import tweepy

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

input_query = "springbreak"  # Change this string to the topic you want to search tweets

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)