想要在某些 channel 统计数据中删除youtube,并了解其工作原理。并且比作csv作以下分析。使用this video创建和学习。
我有2个文件:
main.py,youtube_statistics
main.py
from youtube_statistics import YTstats
API_KEY = "xxx"
channel_id = "xxx"
yt = YTstats(API_KEY, channel_id)
yt.get_channel_statistics()
youtube_statistics.py
class YTstats:
def_init_(self, api_key, channel_id)
self.api_key = api_key
self.channel_id = channel_id
self.channel_statistics = None
def get_channel_statistics(self):
url = f'https://www.googleapis.com/youtube/v3/channels?part=statistics&id={self.channel_id}&key={self.api_key}'
print(url)
这不是全部代码,而是尝试运行主要的错误:
Traceback (most recent call last):
File "D:/!Python/YouTube API/main.py", line 1, in <module>
from youtube_statistics import YTstats
File "D:\!Python\YouTube API\youtube_statistics.py", line 1, in <module>
class YTstats:
File "D:\!Python\YouTube API\youtube_statistics.py", line 2, in YTstats
def_init_(self, api_key, channel_id)
NameError: name 'def_init_' is not defined
怎么了,怎么解决?在视频上一切正常。
谢谢。
最佳答案
在python中,缩进很重要。您的youtube_statistics.py文件缩进了错误的位置。特别是类初始化被声明为错误。这是固定版本:
class YTstats:
def __init__(self, api_key, channel_id):
self.api_key = api_key
self.channel_id = channel_id
self.channel_statistics = None
def get_channel_statistics(self):
url = f'https://www.googleapis.com/youtube/v3/channels?part=statistics&id={self.channel_id}&key={self.api_key}'
print(url)