with open(csv_filename, 'w', newline='', encoding='utf-8') as
csv_file:
writer = csv.writer(csv_file)
writer.writerow(['Comment'])
for comment in comments:
writer.writerow([comment])
with open(json_filename, 'w', encoding='utf-8') as json_file:
json.dump(comments, json_file, ensure_ascii=False, indent=4)
print(f"Comments saved to {csv_filename} and {json_filename}")
for search_result in search_response.get('items', []):
if search_result['id']['kind'] == 'youtube#video':
video_id = search_result['id']['videoId']
comments = get_video_comments(video_id)
if comments:
save_comments(video_id, comments, query)
except Exception as e:
print("An error occurred while searching for videos:", e)
search_query = input("Enter your search query: ").strip().replace(" ",
"+")
try:
while True:
search_videos(search_query)
user_input = input("Press 'q' to quit, or press Enter to continue
fetching comments for the next video: ")
if user_input.lower() == 'q':
break
except KeyboardInterrupt:
print("\nProgram interrupted by the user.")