如何以编程方式从频道链接获取 YouTube 频道 ID

问题描述 投票:0回答:1

我需要获取 youtube 频道的频道 ID(以使用 youtube 数据 api)以及频道的链接。 问题是 YouTube 频道的链接有几种类型。 第一个是频道 ID 在链接中的位置:

https://www.youtube.com/channel/UC-lHJZR3Gqxm24_Vd_AJ5Yw

这没问题,因为我可以从链接中提取频道 ID。 问题出在第二个:

https://www.youtube.com/@PewDiePie

这是您通常获得的 YouTube 频道链接,它有一个 @ 而不是频道 ID。

当我使用 youtube api 获取不太受欢迎的频道的频道 ID 时,我得到了错误的结果:

https://www.googleapis.com/youtube/v3/search?part=snippet&q=@pewdiepie6&type=channel&key=[API_KEY]

我想使用 @pewdiepie6 句柄获取频道信息和频道 ID。

但我通过 @pewdiepie6802 账号获得了更受欢迎的频道。 另外,我因此获得了超过 1 个频道。

{
  "kind": "youtube#searchListResponse",
  "etag": "ecshCA97Jd0CuntIj9NzN3UF37U",
  "nextPageToken": "CAUQAA",
  "regionCode": "CH",
  "pageInfo": {
    "totalResults": 51,
    "resultsPerPage": 5
  },
  "items": [
    {
      "kind": "youtube#searchResult",
      "etag": "x4yStPuXhfUUIaev-BCMiq1JQ4E",
      "id": {
        "kind": "youtube#channel",
        "channelId": "UCnJABWFF_vWuPgPjGjVJF8A"
      },
      "snippet": {
        "publishedAt": "2018-03-02T14:07:51Z",
        "channelId": "UCnJABWFF_vWuPgPjGjVJF8A",
        "title": "PewDiePie",
        "description": "",
        "thumbnails": {
          "default": {
            "url": "https://yt3.ggpht.com/ytc/AMLnZu84CCwyUjzgQkuvN7ms2JUCU0EgofxrTdhZLZKs=s88-c-k-c0xffffffff-no-rj-mo"
          },
          "medium": {
            "url": "https://yt3.ggpht.com/ytc/AMLnZu84CCwyUjzgQkuvN7ms2JUCU0EgofxrTdhZLZKs=s240-c-k-c0xffffffff-no-rj-mo"
          },
          "high": {
            "url": "https://yt3.ggpht.com/ytc/AMLnZu84CCwyUjzgQkuvN7ms2JUCU0EgofxrTdhZLZKs=s800-c-k-c0xffffffff-no-rj-mo"
          }
        },
        "channelTitle": "PewDiePie",
        "liveBroadcastContent": "none",
        "publishTime": "2018-03-02T14:07:51Z"
      }
    },
    {
      "kind": "youtube#searchResult",
      "etag": "2et4mkoeeilV2DhJRH7M5xlgnuQ",
      "id": {
        "kind": "youtube#channel",
        "channelId": "UCSn7KcaNdOrVf4aH-6OdFMg"
      },
      "snippet": {
        "publishedAt": "2017-04-23T06:06:06Z",
        "channelId": "UCSn7KcaNdOrVf4aH-6OdFMg",
        "title": "PewDiePie 6",
        "description": "",
        "thumbnails": {
          "default": {
            "url": "https://yt3.ggpht.com/ytc/AMLnZu8OB0Z-JNTYnB2Q0KLoV6LjvpUYpYfN0sfAMgfT=s88-c-k-c0xffffffff-no-rj-mo"
          },
          "medium": {
            "url": "https://yt3.ggpht.com/ytc/AMLnZu8OB0Z-JNTYnB2Q0KLoV6LjvpUYpYfN0sfAMgfT=s240-c-k-c0xffffffff-no-rj-mo"
          },
          "high": {
            "url": "https://yt3.ggpht.com/ytc/AMLnZu8OB0Z-JNTYnB2Q0KLoV6LjvpUYpYfN0sfAMgfT=s800-c-k-c0xffffffff-no-rj-mo"
          }
        },
        "channelTitle": "PewDiePie 6",
        "liveBroadcastContent": "none",
        "publishTime": "2017-04-23T06:06:06Z"
      }
    },
    {
      "kind": "youtube#searchResult",
      "etag": "88l8OAYbY0S8XY9NbWcqW2QtJJ4",
      "id": {
        "kind": "youtube#channel",
        "channelId": "UCpqNuOLjibXIO43tJbZ-s3w"
      },
      "snippet": {
        "publishedAt": "2017-06-17T21:56:28Z",
        "channelId": "UCpqNuOLjibXIO43tJbZ-s3w",
        "title": "PewDiePie",
        "description": "",
        "thumbnails": {
          "default": {
            "url": "https://yt3.ggpht.com/ytc/AMLnZu-GIq-DWy8zPGzPmQnb5LHvR_MlnlK3t-G5-45i=s88-c-k-c0xffffffff-no-rj-mo"
          },
          "medium": {
            "url": "https://yt3.ggpht.com/ytc/AMLnZu-GIq-DWy8zPGzPmQnb5LHvR_MlnlK3t-G5-45i=s240-c-k-c0xffffffff-no-rj-mo"
          },
          "high": {
            "url": "https://yt3.ggpht.com/ytc/AMLnZu-GIq-DWy8zPGzPmQnb5LHvR_MlnlK3t-G5-45i=s800-c-k-c0xffffffff-no-rj-mo"
          }
        },
        "channelTitle": "PewDiePie",
        "liveBroadcastContent": "none",
        "publishTime": "2017-06-17T21:56:28Z"
      }
    },
    {
      "kind": "youtube#searchResult",
      "etag": "MItPbLglqDfEkQ-fXSbQef7js2c",
      "id": {
        "kind": "youtube#channel",
        "channelId": "UCQXfsdNRgLL0Cz67stMFNVQ"
      },
      "snippet": {
        "publishedAt": "2018-03-31T09:27:20Z",
        "channelId": "UCQXfsdNRgLL0Cz67stMFNVQ",
        "title": "Marian Pewdiepie 6",
        "description": "Nu voi mai posta mai mult de 1 an căci nu prea am vhef Dar totuși dați subscribe.",
        "thumbnails": {
          "default": {
            "url": "https://yt3.ggpht.com/ytc/AMLnZu9G8MRCxqrk5dCx9T6lxjWpaAL62QuD54LGpgV6=s88-c-k-c0xffffffff-no-rj-mo"
          },
          "medium": {
            "url": "https://yt3.ggpht.com/ytc/AMLnZu9G8MRCxqrk5dCx9T6lxjWpaAL62QuD54LGpgV6=s240-c-k-c0xffffffff-no-rj-mo"
          },
          "high": {
            "url": "https://yt3.ggpht.com/ytc/AMLnZu9G8MRCxqrk5dCx9T6lxjWpaAL62QuD54LGpgV6=s800-c-k-c0xffffffff-no-rj-mo"
          }
        },
        "channelTitle": "Marian Pewdiepie 6",
        "liveBroadcastContent": "upcoming",
        "publishTime": "2018-03-31T09:27:20Z"
      }
    },
    {
      "kind": "youtube#searchResult",
      "etag": "_yNYVPOhK2ZEn0yq580TFty6AHU",
      "id": {
        "kind": "youtube#channel",
        "channelId": "UCR8SFIzFOxf50N0lYER8_-w"
      },
      "snippet": {
        "publishedAt": "2019-03-09T20:56:38Z",
        "channelId": "UCR8SFIzFOxf50N0lYER8_-w",
        "title": "Go sub to Pewdiepie 6",
        "description": "",
        "thumbnails": {
          "default": {
            "url": "https://yt3.ggpht.com/ytc/AMLnZu_3fwpv9uhEyzb_bUgNuzETQHpdgvKv-xDSYQ=s88-c-k-c0xffffffff-no-rj-mo"
          },
          "medium": {
            "url": "https://yt3.ggpht.com/ytc/AMLnZu_3fwpv9uhEyzb_bUgNuzETQHpdgvKv-xDSYQ=s240-c-k-c0xffffffff-no-rj-mo"
          },
          "high": {
            "url": "https://yt3.ggpht.com/ytc/AMLnZu_3fwpv9uhEyzb_bUgNuzETQHpdgvKv-xDSYQ=s800-c-k-c0xffffffff-no-rj-mo"
          }
        },
        "channelTitle": "Go sub to Pewdiepie 6",
        "liveBroadcastContent": "none",
        "publishTime": "2019-03-09T20:56:38Z"
      }
    }
  ]
}

我还尝试从页面的 javascript 中提取频道 ID,但使用的是 Beautiful Soup。 但我无法让它返回正确的 javascript。

还有第二个带有 forUsername 参数的 api 链接。 但通过此链接,它尝试寻找在不是我想要的频道上上传的用户。

最终我想要一个安全的方法来获取带有频道链接的频道 ID。 https://www.youtube.com/@pewdiepie6 => UCl-0ct6u2AAfsqGGgVh5dEw

(我从 externalid 页面的 @pewdiepie6 javascript 文件中手动获取此文件)

javascript youtube youtube-data-api
1个回答
1
投票

Youtube Data api 仍然不支持 Handle。因此,目前最安全的方法是curl或file_get_contents。 file_get_contents 获取频道 id 的示例 php 代码:

<?php
  $connect= file_get_contents("https://www.youtube.com/@pewdiepie6");
  $channeli = explode('<meta property="og:site_name" content="YouTube"><meta property="og:url" content="', $connect);   
  $channeli = explode('">', $channeli[1]); 
  $channelidurl="$channeli[0]";
  echo " Channel ID url: $channelidurl";
?>

示例在此页面上,它使用与我上面提到的相同逻辑相同的方法拉取频道 ID url:Youtube Monetization Checker https://ytlarge.com/youtube/monetization-checker/ 目前没有其他方法。

© www.soinside.com 2019 - 2024. All rights reserved.