Home > Blockchain >  Tweepy Api Python Response, need help decoding the response
Tweepy Api Python Response, need help decoding the response

Time:08-19

I trust all is well with you and yours. Thank you for taking a moment to read through this and I apologize if this is a repeat (if it is point me to the right spot and I will read through that!)

I am trying to hit the twitter api via tweepy (cause im to new to figure out python and the twitter official api) and return a result in a useable format.

import Auth_Codes
import json

twitter_auth_keys = {
    "consumer_key"          : Auth_Codes.consumer_key,
    "consumer_secret"       : Auth_Codes.consumer_secret,
    "access_token"          : Auth_Codes.access_token,
    "access_token_secret"   : Auth_Codes.access_token_secret
}

auth = tweepy.OAuthHandler(
    twitter_auth_keys["consumer_key"],
    twitter_auth_keys["consumer_secret"]
)

auth.set_access_token(
    twitter_auth_keys["access_token"],
    twitter_auth_keys["access_token_secret"]
)

api = tweepy.API(auth)

#api.search_tweets(q = "Aztar")

searched_tweets = [tweet for tweet in tweepy.Cursor(api.search_tweets,
                                                    q = "What you want to search",
                                                    lang = 'en',
                                                    result_type = 'recent',
                                                    count = 1)
                   .items(1)]
print(searched_tweets)
print(type(searched_tweets))

when this is executed, I get a very large response that I cannot fully post here. it is also type: <class 'list'>

I hope that added the spoiler button as intended. My issue is that I have tried in several different ways to convert this into an actual json, and I am struggling as every guide I am following online leads me to a dead end (granted I am learning lots!). In node.js, I would normally leverage a map and sort it that way. Is there something similar I can do here? Not all the data is relevant to me.

Thanks in advance, and really sorry about not knowing how to add a spoiler button if it is at all possible.

I have added the following to it:

searched_tweets_dict = json.loads(searched_tweets)
print(searched_tweets_dict)

and the result is the following error code:

Traceback (most recent call last):
  File "E:\Dropbox\Backup\Github\Python\Mid_Journey\Search.py", line 33, in <module>
    searched_tweets_dict = json.loads(searched_tweets)
  File "C:\Pthyon_3.10\lib\json\__init__.py", line 339, in loads
    raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not list

CodePudding user response:

Why are you using a Cursor if you are only requesting one tweet?

And why don't you just use the generator instead of creating that list?

Anyway, the json object is already included in the Tweepy objects (._json).

cursor = tweepy.Cursor(
    api.search_tweets, 
    q = "What you want to search", 
    lang = 'en', 
    result_type = 'recent', 
    count = 1
)

for tweet in cursor.items(1):
    print(tweet._json)
  • Related