user8794562
user8794562

Reputation:

How to parse video_views from instagram json

I'am trying to parse likes count, comments count, video_views comment from json instagram, and with likes and comments there is no problems, but I cannot understand how to get video_views, be cause likes and comments count have connection with like and comments objects accordingly, but video_views dont. Every time I get keyerror by video_views

import urllib.request
import simplejson as json

import urllib.request
url = 'https://www.instagram.com/mcgregor_best/media/'
count = int

response = urllib.request.urlopen(url).read().decode('UTF-8')
json_obj = json.loads(response)


for item in json_obj['items']:
   print(item['likes']['count'])
for item in json_obj['items']:
    print(item['comments']['count'])
for item in json_obj['items']:
    print(item['video_views'])

Upvotes: 0

Views: 290

Answers (1)

chasmani
chasmani

Reputation: 2510

Not every item has a video_views key in the json. Replace print(item['video_views']) with:

print(item.get("video_views", None))

This will give you the value of video_views if it exists as a key, and otherwise give you None.

Upvotes: 1

Related Questions