【问题标题】:Error accessing Youtube Data API using Python使用 Python 访问 Youtube 数据 API 时出错
【发布时间】:2020-08-09 09:26:36
【问题描述】:

我正在关注https://www.geeksforgeeks.org/youtube-data-api-set-1/ 此处的教程。运行以下代码后,我收到“No module named 'apiclient'”错误。我也尝试使用“来自 googleapiclient 导入发现”,但这也给出了错误。有没有人可以尝试替代方案?

我已经导入了 pip install --upgrade google-api-python-client

不胜感激任何帮助/建议!

代码如下:

from apiclient.discovery import build 

# Arguments that need to passed to the build function 
DEVELOPER_KEY = "your_API_Key"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"

# creating Youtube Resource Object 
youtube_object = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, 
                                        developerKey = DEVELOPER_KEY) 


def youtube_search_keyword(query, max_results): 

    # calling the search.list method to 
    # retrieve youtube search results 
    search_keyword = youtube_object.search().list(q = query, part = "id, snippet", 
                                            maxResults = max_results).execute() 

    # extracting the results from search response 
    results = search_keyword.get("items", []) 

    # empty list to store video, 
    # channel, playlist metadata 
    videos = [] 
    playlists = [] 
    channels = [] 

    # extracting required info from each result object 
    for result in results: 
        # video result object 
        if result['id']['kind'] == "youtube# video": 
            videos.append("% s (% s) (% s) (% s)" % (result["snippet"]["title"], 
                            result["id"]["videoId"], result['snippet']['description'], 
                            result['snippet']['thumbnails']['default']['url'])) 

        # playlist result object 
        elif result['id']['kind'] == "youtube# playlist": 
            playlists.append("% s (% s) (% s) (% s)" % (result["snippet"]["title"], 
                                result["id"]["playlistId"], 
                                result['snippet']['description'], 
                                result['snippet']['thumbnails']['default']['url'])) 

        # channel result object 
        elif result['id']['kind'] == "youtube# channel": 
            channels.append("% s (% s) (% s) (% s)" % (result["snippet"]["title"], 
                                result["id"]["channelId"], 
                                result['snippet']['description'], 
                                result['snippet']['thumbnails']['default']['url'])) 

    print("Videos:\n", "\n".join(videos), "\n") 
    print("Channels:\n", "\n".join(channels), "\n") 
    print("Playlists:\n", "\n".join(playlists), "\n") 

if __name__ == "__main__": 
    youtube_search_keyword('Geeksforgeeks', max_results = 10) 

【问题讨论】:

  • 请发布完整的错误信息。为了使帖子更易于阅读,您可以删除from apiclient.discovery import build下面的所有代码,因为错误是由导入行引起的,而不是它下面的代码。

标签: python youtube-data-api


【解决方案1】:

有了这些信息,很难说问题出在哪里。但有时我在使用 pip (Python2) 安装某些东西然后尝试在 Python3 中导入模块时一直在碰壁,反之亦然。

因此,如果您使用 Python3 运行脚本,请尝试使用 pip3 install --upgrade google-api-python-client 安装包

【讨论】:

    【解决方案2】:

    在此处试用 YouTube 文档:

    https://developers.google.com/youtube/v3/code_samples

    他们在最近更新的 Slackware_64 14.2 上为我工作

    我在 Python 3.8 中使用它们。由于可能还安装了 Python 版本 2,因此我确保在 Interpreter 行中使用它:

    !/usr/bin/python3.8

    和pip一样,我用pip3.8安装依赖

    我从源代码安装了 Python。 python3.8 --version Python 3.8.2

    您也可以在此处观看此视频:

    https://www.youtube.com/watch?v=qDWtB2q_09g

    它解释了如何使用 YouTube 的 API 浏览器。您可以直接从那里复制代码示例。上面的视频涵盖了 Android,但关于使用 YouTube 的 API Explorer 的概念同样适用于 Python。

    我同意之前关于版本控制的回答。

    【讨论】:

      猜你喜欢
      • 2016-06-02
      • 1970-01-01
      • 2020-02-24
      • 2018-01-05
      • 2014-09-10
      • 2022-07-27
      • 2018-10-30
      • 2018-05-26
      • 1970-01-01
      相关资源
      最近更新 更多