【问题标题】:I am getting an error while using Python YouTube API使用 Python YouTube API 时出现错误
【发布时间】:2022-07-27 23:51:05
【问题描述】:

我正在尝试创建一个 Python 程序来获取频道统计信息,但是当我运行它时,YouTube API 网站会给出以下输出(错误):

{
  "error": {
    "code": 400,
    "message": "'statisitcs'",
    "errors": [
      {
        "message": "'statisitcs'",
        "domain": "youtube.part",
        "reason": "unknownPart",
        "location": "part",
        "locationType": "parameter"
      }
    ]
  }
}

这是我的代码:

class YTstats:
    def __init__(self, api_key, channel_id):
        self.api_key = api_key
        self.channel_id = channel_id
        self.channel_stats = None

    def get_channel_statistics(self):
        url = f'https://www.googleapis.com/youtube/v3/channels?part=statisitcs&id={self.channel_id}&key={self.api_key}'
        print(url)

API_KEY = 'I cannot share my api key so I am not showing it but it is in my code'
yt = YTstats(API_KEY, 'UCbXgNpp0jedKWcQiULLbDTA')
yt.get_channel_statistics()

【问题讨论】:

    标签: python youtube


    【解决方案1】:

    这个问题现在已经解决了(这是一个错字)

    【讨论】:

      【解决方案2】:

      part=statisitcs 这里有一个错字——正如你在your comment 中提到的那样——在仔细查看了你在问题中提供的代码之后。

      下次,仔细检查您的代码并尝试使用 YouTube 数据 API 文档中的 try-it demo 功能复制错误。


      我确实得到了您提供的 channel_id 的统计数据 - 即:UCbXgNpp0jedKWcQiULLbDTA

      这些是他们的统计数据:

      "statistics": {
          "viewCount": "5642720",
          "subscriberCount": "98400",
          "hiddenSubscriberCount": false,
          "videoCount": "158"
        }
      }
      

      See the demo here.

      以下是完整回复:

      {
        "kind": "youtube#channelListResponse",
        "etag": "lG-nYlbLnN81gtjVKe1zKPW6v7A",
        "pageInfo": {
          "totalResults": 1,
          "resultsPerPage": 5
        },
        "items": [
          {
            "kind": "youtube#channel",
            "etag": "j4Fo8qKbWrLHnQYB8sCI8_I4v9A",
            "id": "UCbXgNpp0jedKWcQiULLbDTA",
            "snippet": {
              "title": "Python Engineer",
              "description": "Free Python and Machine Learning Tutorials!\n\nHi, I'm Patrick. I’m a passionate Software Engineer who loves Machine Learning, Computer Vision, and Data Science. I create free content in order to help more people get into those fields. If you have any questions, feedback, or comments, just shoot me a message! I am happy to talk to you :)\n\nIf you like my content, please subscribe to the channel!\n\nPlease check out my website for more information:\nhttps://www.python-engineer.com\n\nIf you find these videos useful and would like to support my work you can find me on Patreon:\nhttps://www.patreon.com/patrickloeber\n\nLegal: https://www.python-engineer.com/legal-notice/\n",
              "customUrl": "pythonengineer",
              "publishedAt": "2019-05-03T11:22:33Z",
              "thumbnails": {
                "default": {
                  "url": "https://yt3.ggpht.com/ytc/AKedOLTs-Pvd4mvUi-m2rDLd8bzrKwS5a8C9HnDbkUDzHw=s88-c-k-c0x00ffffff-no-rj",
                  "width": 88,
                  "height": 88
                },
                "medium": {
                  "url": "https://yt3.ggpht.com/ytc/AKedOLTs-Pvd4mvUi-m2rDLd8bzrKwS5a8C9HnDbkUDzHw=s240-c-k-c0x00ffffff-no-rj",
                  "width": 240,
                  "height": 240
                },
                "high": {
                  "url": "https://yt3.ggpht.com/ytc/AKedOLTs-Pvd4mvUi-m2rDLd8bzrKwS5a8C9HnDbkUDzHw=s800-c-k-c0x00ffffff-no-rj",
                  "width": 800,
                  "height": 800
                }
              },
              "localized": {
                "title": "Python Engineer",
                "description": "Free Python and Machine Learning Tutorials!\n\nHi, I'm Patrick. I’m a passionate Software Engineer who loves Machine Learning, Computer Vision, and Data Science. I create free content in order to help more people get into those fields. If you have any questions, feedback, or comments, just shoot me a message! I am happy to talk to you :)\n\nIf you like my content, please subscribe to the channel!\n\nPlease check out my website for more information:\nhttps://www.python-engineer.com\n\nIf you find these videos useful and would like to support my work you can find me on Patreon:\nhttps://www.patreon.com/patrickloeber\n\nLegal: https://www.python-engineer.com/legal-notice/\n"
              }
            },
            "contentDetails": {
              "relatedPlaylists": {
                "likes": "",
                "uploads": "UUbXgNpp0jedKWcQiULLbDTA"
              }
            },
            "statistics": {
              "viewCount": "5642720",
              "subscriberCount": "98400",
              "hiddenSubscriberCount": false,
              "videoCount": "158"
            }
          }
        ]
      }
      

      尝试在您拨打电话后等待几分钟 - 可能是 API 无法检索您请求的数据,因为请求过多或频道本身没有公开可用的统计信息。

      【讨论】:

        猜你喜欢
        • 2015-01-01
        • 2021-12-16
        • 2022-07-15
        • 2012-08-02
        • 1970-01-01
        • 2019-07-08
        • 1970-01-01
        • 1970-01-01
        • 2011-09-11
        相关资源
        最近更新 更多