前提・実現したいこと
InstagramのGraph APIを使用して、
Instagramの分析をしようとしております。
こちらのリンクを参考に進めているのですが、
コメントがうまく抜けません。
コメントのカラムがないのでは、と思っているのですが、
解決方法をご教示頂けますと幸いです。
発生している問題・エラーメッセージ
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-7-58908664bad4> in <module> 31 32 df_likers = posts_likers_to_df(likers) ---> 33 df_commenters = posts_commenters_to_df(commenters) <ipython-input-7-58908664bad4> in posts_commenters_to_df(commenters) 15 # Include username and full_name of commenter in 'comments' list of dicts 16 for i in range(len(commenters)): ---> 17 if len(commenters[i]['comments']) > 0: # checks if there is any comment on the post 18 for j in range(len(commenters[i]['comments'])): 19 # Puts username/full_name one level up KeyError: 'comments'
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-8-ed1249161f9c> in <module> 4 print('Distinct users that liked your posts: ' +str(df_likers.username.nunique())) # nunique() will count distinct values of a col 5 print('---------') ----> 6 print('Total comments on profile: ' + str(df_comment.shape[0])) 7 print('Distinct users that commented your posts: ' +str(df_comment.username.nunique())) NameError: name 'df_comment' is not defined
該当のソースコード
Python
1from InstagramAPI import InstagramAPI 2import pandas as pd 3from pandas.io.json import json_normalize 4 5def login_to_instagram(username, password): 6 api = InstagramAPI(username, password) 7 api.login() 8 9 return api 10 11api = login_to_instagram('XXXX','YYYY') 12 13def get_my_posts(api): 14 '''Retrieve all posts from own profile''' 15 my_posts = [] 16 has_more_posts = True 17 max_id= '' 18 19 while has_more_posts: 20 api.getSelfUserFeed(maxid=max_id) 21 if api.LastJson['more_available'] is not True: 22 has_more_posts = False #stop condition 23 24 max_id = api.LastJson.get('next_max_id','') 25 my_posts.extend(api.LastJson['items']) #merge lists 26# time.sleep(2) # slows down to avoid flooding 27 28 if has_more_posts: 29 print(str(len(my_posts)) + ' posts retrieved so far...') 30 31 print('Total posts retrieved: ' + str(len(my_posts))) 32 33 return my_posts 34 35my_posts = get_my_posts(api) 36 37def get_posts_likers(api, my_posts): 38 '''Retrieve all likers on all posts''' 39 40 likers = [] 41 42 print('wait %.1f minutes' % (len(my_posts)*2/60.)) 43 for i in range(len(my_posts)): 44 m_id = my_posts[i]['id'] 45 api.getMediaLikers(m_id) 46 47 likers += [api.LastJson] 48 49 # Include post_id in likers dict list 50 likers[i]['post_id'] = m_id 51 52# time.sleep(2) 53 print('done') 54 55 return likers 56 57 58likers = get_posts_likers(api, my_posts) 59 60def get_posts_commenters(api, my_posts): 61 '''Retrieve all commenters on all posts ''' 62 63 commenters = [] 64 65 print('wait %.1f minutes' % (len(my_posts)*2/60.)) 66 for i in range(len(my_posts)): 67 m_id = my_posts[i]['id'] 68 api.getMediaComments(m_id) 69 70 commenters += [api.LastJson] 71 72 # Include post_id in commenters dict list 73 commenters[i]['post_id'] = m_id 74 75# time.sleep(2) 76 print('done') 77 78 return commenters 79 80commenters = get_posts_commenters(api, my_posts) 81 82def posts_likers_to_df(likers): 83 '''Transforms likers list of dicts into pandas DataFrame''' 84 85 # Normalize likers by getting the 'users' list and the post_id of each like 86 df_likers = json_normalize(likers, 'users', ['post_id']) 87 88 # Add 'content_type' column to know the rows are likes 89 df_likers['content_type'] = 'like' 90 91 return df_likers 92 93def posts_commenters_to_df(commenters): 94 '''Transforms commenters list of dicts into pandas DataFrame''' 95 96 # Include username and full_name of commenter in 'comments' list of dicts 97 for i in range(len(commenters)): 98 if len(commenters[i]['comments']) > 0: # checks if there is any comment on the post 99 for j in range(len(commenters[i]['comments'])): 100 # Puts username/full_name one level up 101 commenters[i]['comments'][j]['username'] = commenters[i]['comments'][j]['user']['username'] 102 commenters[i]['comments'][j]['full_name'] = commenters[i]['comments'][j]['user']['full_name'] 103 104 # Create DataFrame 105 # Normalize commenters to have 1 row per comment, and gets 'post_id' from parent 106 df_commenters = json_normalize(commenters, 'comments', 'post_id') 107 108 # Get rid of 'user' column as we already handled it above 109 del df_commenters['user'] 110 111 return df_commenters 112 113df_likers = posts_likers_to_df(likers) 114df_commenters = posts_commenters_to_df(commenters) 115 116#一個目のエラー 117 118print('Total posts: ' + str(len(my_posts))) 119print('---------') 120print('Total likes on profile: ' + str(df_likers.shape[0])) #shape[0] represents number of rows 121print('Distinct users that liked your posts: ' +str(df_likers.username.nunique())) # nunique() will count distinct values of a col 122print('---------') 123print('Total comments on profile: ' + str(df_comment.shape[0])) 124print('Distinct users that commented your posts: ' +str(df_comment.username.nunique())) 125 126#二個目のエラー
試したこと
df_comment→df_commenterにしても効果なし。
補足情報(FW/ツールのバージョンなど)
Python 3.8.3
あなたの回答
tips
プレビュー