抓取工具 Python 和 YouTube API
Scraper Python and YouTube API
想抓取 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 初始化被声明为错误。这是固定版本:
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)
想抓取 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 初始化被声明为错误。这是固定版本:
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)