简体   繁体   English

Python Twitter 流媒体时间线

[英]Python Twitter Streaming Timeline

****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' . ****我正在尝试从特定用户的 twitter 时间轴获取信息,并尝试以 Json 格式打印输出,但是我收到了一个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.我是 python 的新手,所以我在尝试解决这个问题时遇到了麻烦,所以任何帮助都将不胜感激。 **** ****

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)

You seem to be mixing up tweepy with twitter , and are possibly getting a bit confused with methods as a result.您似乎将tweepytwitter混为一谈,因此可能对方法有点困惑。 The auth process for tweepy , from your code, should go as follows:来自您的代码的tweepy身份验证过程应如下所示:

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如果,如前所述,您想创建一个推文列表,您可以执行以下操作,而不是在# 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. Twitter 允许您重置它们,我建议您尽快这样做 - 编辑您的帖子是不够的,因为人们仍然可以阅读您的编辑历史记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM