如何从instagram json解析视频视图

2024-03-29 11:07:19 发布

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

我试图从json instagram解析likes count,comments count,video\u views comment,使用likes和comments没有问题,但我不明白如何获取video_views,因为likes和comments计数与like和comments对象有相应的联系,但video_views没有。每次我通过视频查看keyror

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'])

Tags: inimportjsonobjforrequestvideocount
1条回答
网友
1楼 · 发布于 2024-03-29 11:07:19

并不是每个项目都有一个json中的video\u views键。将print(item['video_views'])替换为:

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

如果视频作为一个键存在,这将为您提供video_views的值,否则将不提供任何值。在

相关问题 更多 >