AttributeError:'str'对象没有属性'request' - youtube data api

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

AttributeError:“str”对象没有属性“request” - youtube data api

我正在尝试从 python 中的 youtube data api v3 获取数据,但收到 AttributeError: 'str' object has no attribute 'request' 这是代码

from googleapiclient.discovery import build


youtube = build("youtube", "v3", api_key)

def get_channel_stats(youtube, channel_id):
    request = youtube.channels().list(
           part="snippet,contentDetails,statistics",
           forUsername=channel_id
    )
    response = request.execute()
    return response

get_channel_stats(youtube, channel_id)

得到这个

AttributeError

Cell In [14], line 1
----> 1 get_channel_stats(youtube, channel_id)

Cell In [13], line 6, in get_channel_stats(youtube, channel_id)
      1 def get_channel_stats(youtube, channel_id):
      2     request = youtube.channels().list(
      3            part="snippet,contentDetails,statistics",
      4            forUsername=channel_id
      5     )
----> 6     response = request.execute()
      7     return response

File c:\Python310\lib\site-packages\googleapiclient\_helpers.py:130, in positional.<locals>.positional_decorator.<locals>.positional_wrapper(*args, **kwargs)
    128     elif positional_parameters_enforcement == POSITIONAL_WARNING:
    129         logger.warning(message)
--> 130 return wrapped(*args, **kwargs)

File c:\Python310\lib\site-packages\googleapiclient\http.py:923, in HttpRequest.execute(self, http, num_retries)
    920     self.headers["content-length"] = str(len(self.body))
    922 # Handle retries for server-side errors.
--> 923 resp, content = _retry_request(
    924     http,
    925     num_retries,
...
--> 191     resp, content = http.request(uri, method, *args, **kwargs)
    192 # Retry on SSL errors and socket timeout errors.
    193 except _ssl_SSLError as ssl_error:

AttributeError: 'str' object has no attribute 'request'

我从 pip 安装了 googleapiclient 我找不到与 youtube data api 相关的正确解决方案

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

你找到解决办法了吗?我也遇到过类似的问题,

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