merhaba bu konumuzda youtube kanalımızdaki videomuza yapılan tüm yorumlari csv dosyasına nasıl kaydedecegimizi göstereceğim öncellikle Google api giderek youtube data api v3 hizmetini aktif edin Bir adet Oauth token oluşturun ve json dosyası olarak indirin.
bu kodu yorum.py olarak kaydedin oluşturdugumuz Oauth json dosyasılnı ayn dizine atalım
import csv
import os
import google.oauth2.credentials
from google.auth.transport.requests import Request
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
# Bu fonksiyon, Google API'ye erişim için yetkilendirme sağlar
def get_authenticated_service():
creds = None
# Erişim tokenlarını diskte depolamak için dosya yolu
token_path = 'token.json'
# Eğer daha önce yetkilendirme yapılmışsa, onu yükle
if os.path.exists(token_path):
creds = google.oauth2.credentials.Credentials.from_authorized_user_file(token_path)
# Token geçerli değilse veya yoksa, kullanıcıyı yetkilendir
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'client_secrets.json',
['https://www.googleapis.com/auth/youtube.force-ssl']
)
creds = flow.run_local_server(port=0)
# Yetkilendirme bilgilerini diske kaydet
with open(token_path, 'w') as token:
token.write(creds.to_json())
# Yetkilendirilmiş bir YouTube servisi döndür
return build('youtube', 'v3', credentials=creds)
def get_video_comments(youtube, video_id):
comments = []
next_page_token = None
while True:
response = youtube.commentThreads().list(
part='snippet',
videoId=video_id,
pageToken=next_page_token,
maxResults=100 # Bir sayfada alınacak yorum sayısı
).execute()
for item in response['items']:
comment = item['snippet']['topLevelComment']['snippet']['textDisplay']
comments.append(comment)
next_page_token = response.get('nextPageToken')
if not next_page_token:
break
return comments
def write_comments_to_csv(comments, output_file):
with open(output_file, 'w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
writer.writerow(['Yorum'])
for comment in comments:
writer.writerow([comment])
# Google API'ye erişim için yetkilendirilmiş bir servis oluştur
youtube_service = get_authenticated_service()
# Yorumları almak istediğiniz video ID'sini belirtin
video_id = 'JmPwDX0JDTY'
# Videoya yapılan tüm yorumları al
comments = get_video_comments(youtube_service, video_id)
# CSV dosyasına yorumları yaz
output_file = 'video_comments.csv'
write_comments_to_csv(comments, output_file)
print(f"Yorumlar {output_file} dosyasına başarıyla yazıldı.")kodun içindkei video id kısmına videonuzu idsini yazarak kodu çlaıştırıyorsunuz çalıştırdıktan sonra sizden sizden google hesabiniza eriişim için izin istyeecek onayladıktan sonra video idini yazdiginiz videonuzu tüm yorumlari csv olarak dosyaya kaydedecektir.