我使用Tweepy获取@UserName发布的所有推文。这是以下代码
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以读取该特定用户的状态数?
答案 0 :(得分:1)
如何跟踪您迭代过多少个状态?我并不认为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数据具有良好的结构,具有如下所示的清晰格式,使其更易于理解。
因此,当我想遍历此列表以查找是否存在x(成就,在这种情况下)时,我使用此函数,它将每次迭代的索引加1。
def achnamefdr(appid,mykey,steamid64,achname):
playerachurl = 'http://api.steampowered.com/ISteamUserStats/GetPlayerAchievements/v0001/?appid=' + str(appid) + '&key=' + mykey + '&steamid=' + steamid64 + '&l=name'
achjson = json.loads(urllib.request.urlopen(playerachurl).read().decode('utf-8'))
achjsonr = achjson['playerstats']['achievements']
index = 0
for ach in achjsonr:
if not ach['name'].lower() == achname.lower():
index += 1
continue
else:
achnamef = ach['name']
return achnamef, index, True
return 'Invalid Achievement!', index, False
答案 1 :(得分:0)
可以通过从 status._json 获取JSON对象然后解析它来完成。
print status._json["statuses_count"]