python - Scraper Python和YouTube API

标签 python api web-scraping youtube

想要在某些 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)

关于python - Scraper Python和YouTube API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61715852/

相关文章:

Python Selenium 'WebDriver' object has no attribute 错误

python - 赋值前引用的局部变量 'servers'

api - FatSecret API - food.get 方法签名无效

javascript - 来自 REST API 的 HTTP 请求

api - 创建 Rails 5 API-Only 应用程序时使用什么进行用户注册?

python - <span> 内没有类的 BS4 文本

r - "Error: not compatible with STRSXP"与 rvest 的 submit_form

python - 如何在keras中实现麦克劳林系列?

python - 重新抽样查询

python - 寻找两个字符串之间最长的完美匹配