2

****I am trying to obtain information from the twitter timeline of a specific user and I am trying to print the output in Json format, however I am getting an AttributeError: 'str' object has no attribute '_json'. I am new to python so I'm having troubles trying to resolve this so any help would be greatly appreciated. ****

Below shows the code that I have at the moment:

from __future__ import absolute_import, print_function

import tweepy
import twitter

def oauth_login():
    # credentials for OAuth
    CONSUMER_KEY = 'woIIbsmhE0LJhGjn7GyeSkeDiU'
    CONSUMER_SECRET = 'H2xSc6E3sGqiHhbNJjZCig5KFYj0UaLy22M6WjhM5gwth7HsWmi'
    OAUTH_TOKEN = '306848945-Kmh3xZDbfhMc7wMHgnBmuRLtmMzs6RN7d62o3x6i8'
    OAUTH_TOKEN_SECRET = 'qpaqkvXQtfrqPkJKnBf09b48TkuTufLwTV02vyTW1kFGunu'

    # Creating the authentication
    auth = twitter.oauth.OAuth( OAUTH_TOKEN,
                                OAUTH_TOKEN_SECRET,
                                CONSUMER_KEY,
                                CONSUMER_SECRET )
    # Twitter instance
    twitter_api = twitter.Twitter(auth=auth)
    return twitter_api

# LogIn
twitter_api = oauth_login()
# Get statuses
statuses = twitter_api.statuses.user_timeline(screen_name='@ladygaga')
# Print text 

for status in statuses:
    print (status['text']._json)
Arpit
  • 47
  • 10
  • "trying to print the output in Json format". Is there a specific reason you want to do this? Typically, Json is used for storage, rather than output. – patrick Feb 01 '17 at 15:49
  • you've got it the wrong way round - `status['text']._json` should be `status._json['text']` – asongtoruin Feb 01 '17 at 15:51
  • Well basically my tutor wants the output in a list, so that is why I tried to add '._json' when trying to print the output but it's giving errors @patrick – Arpit Feb 01 '17 at 15:53
  • 1
    also you shouldn't be giving out your credentials - go into the twitter apps dashboard and reset them ASAP, you've compromised them by giving them out publicly. – asongtoruin Feb 01 '17 at 15:55
  • @asongtoruin thanks but it still gives an attribute error – Arpit Feb 01 '17 at 15:58
  • 1
    Instead of putting your credentials in your code, you can create [environment variables](https://en.wikipedia.org/wiki/Environment_variable) for them ([Linux](http://unix.stackexchange.com/questions/117467/how-to-permanently-set-environmental-variables), [Windows](http://www.computerhope.com/issues/ch000549.htm)) and then read them with Python's [`os.environ`](http://stackoverflow.com/questions/4906977/access-environment-variables-from-python). – Marcus Vinícius Monteiro Feb 01 '17 at 16:33

1 Answers1

1

You seem to be mixing up tweepy with twitter, and are possibly getting a bit confused with methods as a result. The auth process for tweepy, from your code, should go as follows:

import tweepy

def oauth_login():
    # credentials for OAuth
    consumer_key = 'YOUR_KEY'
    consumer_secret = 'YOUR_KEY'
    access_token = 'YOUR_KEY'
    access_token_secret = 'YOUR_KEY'

    # Creating the authentication
    auth = tweepy.OAuthHandler(consumer_key,
                               consumer_secret)
    # Twitter instance
    auth.set_access_token(access_token, access_token_secret)
    return tweepy.API(auth)

# LogIn
twitter_api = oauth_login()
# Get statuses
statuses = twitter_api.user_timeline(screen_name='@ladygaga')

# Print text
for status in statuses:
    print (status._json['text'])

If, as previously mentioned, you want to create a list of tweets, you could do the following rather than everything after # Print text

# Create a list
statuses_list = [status._json['text'] for status in statuses]

And, as mentioned in the comments, you shouldn't every give out your keys publicly. Twitter lets you reset them, which I'd recommend you do as soon as possible - editing your post isn't enough as people can still read your edit history.

asongtoruin
  • 9,794
  • 3
  • 36
  • 47
  • Hey thanks, yeh I will reset it now. I used the code you provided, I am getting an identation error on consumer_key. Whats happening because I can't see anything wrong in what I replaced it with :/ @asongtoruin – Arpit Feb 01 '17 at 16:17
  • @Arpit seems like a typo perhaps - check you've got the colon included at the end of `def oauth_login():`? – asongtoruin Feb 01 '17 at 16:26
  • yeh I double checked, it's so weird getting that error when I can't see any typo errors – Arpit Feb 01 '17 at 16:49
  • 1
    @Arpit can you provide your code now that it's been changed through uploading to somewhere like [pastebin](http://pastebin.com/)? As previously mentioned, make sure you edit out/alter your keys first! – asongtoruin Feb 01 '17 at 16:56
  • @Arpit you haven't copied the data across properly - the `# credentials...` lines need to be spaced in line with the `# Creating the authentication` lines, as shown in the answer. – asongtoruin Feb 01 '17 at 17:25
  • you also don't appear to have closed the bracket in your `statuses=...` line – asongtoruin Feb 01 '17 at 17:26
  • sorry the bracket part was incorrectly pasted. However it still shows same errors – Arpit Feb 01 '17 at 17:32
  • @Arpit look at your indentation - the credentials lines need to be indented to be part of the method. – asongtoruin Feb 01 '17 at 17:34
  • It now shows a unicodeEncoderError: 'charmap' ?? @asongtoruin – Arpit Feb 01 '17 at 17:35
  • @Arpit try changing the last line to `print status._json['text'].encode('utf-8')` - there's some character or other that it's struggling to display. – asongtoruin Feb 01 '17 at 17:41
  • Honestly really appreciating your help aha :). This time it gave SyntaxError: invalid syntax :/ @asongtoruin – Arpit Feb 01 '17 at 17:44
  • my bad, accidentally converted the print call back to Python 2, which I think is the issue - `print (status._json['text'].encode('utf-8'))` – asongtoruin Feb 01 '17 at 17:46
  • Omg it worked!! aha Thank you so much !! Really appreciate it :) Neary spent the whole day trying to fix this! @asongtoruin – Arpit Feb 01 '17 at 17:49
  • @Arpit knew we'd get there in the end :) you can mark this as the answer to your problem using the tick at the left side of my answer, which should (in theory!) help anyone else who stumbles across it – asongtoruin Feb 01 '17 at 17:51
  • Hey, I was just wondering if you have a similar code for getting the followers list and a separate one for followees? (Like a list of their names) @asongtoruin – Arpit Feb 04 '17 at 16:49
  • @Arpit check [the docs](http://docs.tweepy.org/en/v3.5.0/api.html#friendship-methods) - you'll be after `twitter_api.friends_ids` for following and `twitter_api.followers_ids` for followers. If you have specific issues with them, you'll need to post a new question! – asongtoruin Feb 04 '17 at 17:57