Danil Zyryanov
Danil Zyryanov

Reputation: 27

Scraper Python and YouTube API

would like to scrape youtube for some channels stats and lo learn how it works. And than to make csv for the following analysis. Used this video to create and to learn.

I have 2 files: 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)

This is not all code, but tries to run main called errors:

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

What going wrong, how to fix? On video everything is works.

Thank you.

Upvotes: 0

Views: 499

Answers (1)

tayken
tayken

Reputation: 127

In python indentation matters. Your youtube_statistics.py file was indented wrong. Particularly the class initialization was declared wrong. Here is the fixed version:

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)

Upvotes: 1

Related Questions