Lemontigre
Lemontigre

Reputation: 1

Tweepy ```tweet.text``` not retrieving text. V2

I am attempting to create a Twitter Bot that will search for a topic and then analyze the sentiment to provide polarity. This is the first stage: text scraping. Next I will write a script to clean the text, then analyze, and finally provide a pandas DataFrame and charts to depict sentiment. I would like to eventually build a securities/crypto trading bot.

I have edited my script, and here is what I have now:

import...

from tweepy import OAuthHandler


# Twitter API v1.1 (apply for 'Elevated' privileges on Twitter Dev
#auth = tw.OAuthHandler(cons_key, cons_secret)
#auth.set_access_token(access_token, access_secret)
#api = tw.API(auth)

# Twitter Authentication
def getClient():
    client = tw.Client(bearer_token=config.b_token,
                   consumer_key=config.cons_key,
                   consumer_secret=config.cons_secret,
                   access_token=config.access_token)
    return client

# Defining the search function
def searchtweets (client, query):
    tweets = client.search_recent_tweets(query=query, max_results=max_results)

    tweets_data = tweets.data
    results = []

    if not tweet_data is None and len(tweet_data) > 0:
        for tweet in tweet_data:
            obj = {}
            obj['id'] = tweet['id']
            obj['text'] = tweet['text']
            results.append(obj)
    results = tweets
    return tweets

def responses():
    tweets_data = tweets.data
    results = []
    tweets = client.response()
    return tweets

    tweet_data = tweets.data
# what does this do?
#def getTweet(client, id):
#    tweet = client.get_tweet(id, expansions=['author_id'], user_fields=['username'])

# Search Parameters
keyword = input('Enter keyword to search: ')
search = ''+str(keyword)+' lang:en -is:retweet -shoutout -giveaway'
max_results = input('Enter number of results to display (100 max): ')

# Executing Search
client = getClient()
tweets = searchtweets(client, str(search))

# Defining resulting data > dict.
objs = []

if len(tweets) > 0:
    for tweet in tweets:
        #twt = getTweet(client, tweet['id'])
        obj = {}
        #obj['username'] = twt.includes['users'][0].username
        obj['id'] = tweet['id']
        obj['text'] = tweet['text']
        objs.append(obj)

# Displaying data
for tweet in objs:
    print(tweet['text'])

My script was returning the text field until I updated PyCharm; Now it returns NameError: name 'tweet_data' is not defined.

I feel like I am not defining my lists correctly, but I am not sure how to use namedtuples(). Or I am defining my dictionaries correctly, but they do not align with the Twitter API/Tweepy structure of tweets. Could someone please help?

Upvotes: 0

Views: 207

Answers (1)

Harmon758
Harmon758

Reputation: 5157

As noted and as the error says, tweet_data isn't defined. You define a tweets_data instead.

Upvotes: 1

Related Questions