【问题标题】:Scraper Python and YouTube API爬虫 Python 和 YouTube API
【发布时间】:2020-08-26 04:14:13
【问题描述】:

抓取 youtube 以获取某些频道的统计信息,并了解它是如何工作的。而不是为以下分析制作csv。使用this video 进行创作和学习。

我有 2 个文件: main.py,youtube_statistics

ma​​in.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)

这不是所有代码,而是尝试运行 main 调用的错误:

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 api web-scraping youtube


    【解决方案1】:

    在 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)
    

    【讨论】:

    • 谢谢@mechanical_meat,我在回答后发现了错字,但你打败了我! :)
    • 没问题的兄弟!
    猜你喜欢
    • 2019-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-10
    • 1970-01-01
    • 2016-05-31
    相关资源
    最近更新 更多