使用Python从Twitter解析Status JSON以查找statusecon

2024-03-29 14:24:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Tweepy获取@UserName发出的所有tweets。这是下面的代码

import urllib, json
import sys
import tweepy
from tweepy import OAuthHandler

def twitter_fetch(screen_name = "prateek",maxnumtweets=10):

consumer_token = "" #Keys removed for security
consumer_secret = ""
access_token = ""
access_secret = ""

auth = tweepy.OAuthHandler(consumer_token,consumer_secret)
auth.set_access_token(access_token,access_secret)

api  = tweepy.API(auth)

for status in tweepy.Cursor(api.user_timeline,id=screen_name).items(1):
    print status['statuses_count']
    print '\n'



if __name__ == '__main__':
twitter_fetch('BarackObama',200)

如何正确解析JSON以读取特定用户的状态数?在


Tags: nameimporttokenauthapiforsecretaccess
2条回答

这可以通过从status获取JSON对象,然后对其进行解析来完成。。在

print status._json["statuses_count"]

有什么方法可以跟踪你迭代了多少个状态?我不确定tweepy是怎么工作的,但是用这样的方法:

statuses = 0
for status in tweepy.Cursor(api.user_timeline,id=screen_name).items(1):
    print status['statuses_count']
    statuses += 1
    print '\n'
return statuses

通常JSON数据有一个很好的结构,有如下清晰的格式,这使得它更容易理解。在

http://i.imgur.com/apLWCh5.png

所以,当我想遍历这个列表以确定是否存在一个x时(在本例中,是成就),我使用这个函数,它在每次迭代中都会在索引中添加1。在

^{pr2}$

相关问题 更多 >