Home > front end >  Get the value of json from Youtube API
Get the value of json from Youtube API

Time:12-21

I'm trying to get the value of title, but after I fetch the data, still cannot get the value of it.

import json
import requests
class yt():
    def get_html_to_json(self, path):
        api_url = path
        r = requests.get(api_url)
        if r.status_code == requests.codes.ok:
            data = r.json()
        else:
            data = None
        return data

path = "https://www.googleapis.com/youtube/v3/videospart=snippet,statistics&id=qmN1Gf8rRc8&key=myapitoken"
datas = yt()
dict = datas.get_html_to_json(path)
result = rr.get("title", "not found")
print(result)

Since the value of title is in the dictionary of the list of the dictionary. May I ask if there is any fast way to fetch it, please?

Trying to get the value of title(Japan in 8K- 1 Hour Relaxing Aerial Film) in following json result:

{
  "kind": "youtube#videoListResponse",
  "etag": "DfROQGmVyiyjDrtVirknUpTZYBk",
  "items": [
    {
      "kind": "youtube#video",
      "etag": "uk-f4nXndwrnK0caSbVn6bqxxVE",
      "id": "qmN1Gf8rRc8",
      "snippet": {
        "publishedAt": "2020-01-02T10:16:47Z",
        "channelId": "UCJEjzXRDQN64Iu2elIRdbMQ",
        "title": "Japan in 8K- 1 Hour Relaxing Aerial Film",
        "description": "1-hour 8K aerial video of Japan shot between 2018-2019. Filming locations (In order of appearance): Izu, Yaskushima, Kyoto, Hakodate, Tokyo, Yokohama, Enoshima, Nagoya.We will continue to film across Japan in 2020 and beyond in 8K.\n\n2018年から2019年にかけて撮影した日本各地の空撮素材を1時間の映像にまとめました。ゆったりとした音楽と共にお楽しみください。撮影地:伊豆、屋久島、京都、函館、東京、横浜、江ノ島、名古屋\n\n※Unauthorized usage is strictly prohibited. Unauthorized re-upload may result in the deletion of uploader's channel.(一切の無断使用を禁止いたします。)",
        "thumbnails": {
          "default": {
            "url": "https://i.ytimg.com/vi/qmN1Gf8rRc8/default.jpg",
            "width": 120,
            "height": 90
          },
          "medium": {
            "url": "https://i.ytimg.com/vi/qmN1Gf8rRc8/mqdefault.jpg",
            "width": 320,
            "height": 180
          },
          "high": {
            "url": "https://i.ytimg.com/vi/qmN1Gf8rRc8/hqdefault.jpg",
            "width": 480,
            "height": 360
          },
          "standard": {
            "url": "https://i.ytimg.com/vi/qmN1Gf8rRc8/sddefault.jpg",
            "width": 640,
            "height": 480
          },
          "maxres": {
            "url": "https://i.ytimg.com/vi/qmN1Gf8rRc8/maxresdefault.jpg",
            "width": 1280,
            "height": 720
          }
        },
        "channelTitle": "Armadas",
        "tags": [
          "Japan",
          "8K",
          "日本",
          "空撮",
          "Aerial",
          "Drone",
          "ドローン",
          "RED",
          "Monstro",
          "4K",
          "リラックス",
          "Relaxing",
          "Tokyo",
          "Yokohama",
          "Nagoya",
          "Yakushima",
          "Izu",
          "Hokkaido",
          "Hakodate",
          "Shibuya",
          "Shinjuku",
          "Kyoto"
        ],
        "categoryId": "22",
        "liveBroadcastContent": "none",
        "localized": {
          "title": "Japan in 8K- 1 Hour Relaxing Aerial Film",
          "description": "1-hour 8K aerial video of Japan shot between 2018-2019. Filming locations (In order of appearance): Izu, Yaskushima, Kyoto, Hakodate, Tokyo, Yokohama, Enoshima, Nagoya.We will continue to film across Japan in 2020 and beyond in 8K.\n\n2018年から2019年にかけて撮影した日本各地の空撮素材を1時間の映像にまとめました。ゆったりとした音楽と共にお楽しみください。撮影地:伊豆、屋久島、京都、函館、東京、横浜、江ノ島、名古屋\n\n※Unauthorized usage is strictly prohibited. Unauthorized re-upload may result in the deletion of uploader's channel.(一切の無断使用を禁止いたします。)"
        }
      },
      "statistics": {
        "viewCount": "6415161",
        "likeCount": "51689",
        "favoriteCount": "0",
        "commentCount": "1639"
      }
    }
  ],
  "pageInfo": {
    "totalResults": 1,
    "resultsPerPage": 1
  }
}

CodePudding user response:

try this

var title=datas['items'][0]['snippet']['title'];

CodePudding user response:

Please try this its working for me

from urllib.request import urlopen
import json

url = "https://www.googleapis.com/youtube/v3/videos?id=7blNxSScSQc&key=myapitoken&fields=items(id,snippet(channelId,title,categoryId),statistics)&part=snippet,statistics"
response = urlopen(url)
data_json = json.loads(response.read())
print(data_json)

output click here

please make sure your url is working in blowser

  • Related