使用Python访问Youtube Data API时出错

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

我正在这里https://www.geeksforgeeks.org/youtube-data-api-set-1/的教程中。运行以下代码后,出现“没有名为'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) 

python youtube-data-api
1个回答
0
投票

有了这些信息,很难说出问题所在。但是有时候在用pip(Python2)安装某些东西时,我总是一头撞墙,然后尝试在Python3中导入模块,反之亦然。

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

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