Answer the question
In order to leave comments, you need to log in
I'm trying to parse and classify comments from youtube, I get such errors, maybe someone knows?
Here is the code:
from apiclient.discovery import build
import argparse
import unidecode
import time
import os
import pandas as pd
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from textblob import TextBlob
import re
def sentiment(polarity):
if polarity < 0:
p="Negative"
elif polarity > 0:
p="Positive"
else:
p="Neutral"
return p
def scrape_comments(youtube_video_url,Movie_name):
s=Service(r"C:\\Users\\Olga\\Downloads\\chromedriver_win32\\chromedriver.exe")
chrome_path= r"C:\Users\Olga\Downloads\chromedriver_win32\\chromedriver.exe"
driver = webdriver.Chrome(service=s)
driver.get(youtube_video_url)
driver.maximize_window()
driver.implicitly_wait(50)
driver.execute_script('window.scrollTo(0, 600);')
time.sleep(50)
print ("scrolled")
sort= driver.find_element_by_xpath("""//*[@id="icon_label"]""")
driver.implicitly_wait(15)
sort.click()
sort.time.sleep(10)
topcomments =driver.find_elements_by_xpath("""//*[@id="menu"]/a[1]/paper-item-body/div[1]""")
topcomments.click()
time.sleep(10)
for i in range(0,2):
driver.execute_script("window.scrollTo(0,Math.max(document.documentElement.scrollHeight,document.body.scrollHeight?document.documentElement.clientHeight," +
"document.body.scrollHeight,document.documentElement.clientHeight));");
time.sleep(10)
totalcomments= len(driver.find_elements_by_xpath("""//*[@id="content-text"]"""))
if totalcomments < 20:
index= totalcomments
else:
index= 20
ccount = 0
comments= []
while ccount < index:
try:
comment = driver.find_elements_by_xpath('//*[@id="content-text"]')[ccount].text
ccount=ccount+1
print(comment)
comments.append(comment)
except:
comment = ""
polarity=[]
subjectivity=[]
sentiment_type=[]
for elm in comments:
x=TextBlob(elm)
print(elm)
print("Polarity : "+str(x.sentiment.polarity))
print("Subjectivity :"+str(x.sentiment.subjectivity))
polarity.append(x.sentiment.subjectivity)
subjectivity.append(x.sentiment.subjectivity)
s=sentiment(x.sentiment.polarity)
print("Sentiment Type :"+s)
sentiment_type.append(s)
return driver
dataframe={"comment":comments,"polarity":polarity,"sentiment_type":sentiment_type,"sebjectivity":subjectivity}
df = pd.DataFrame.from_dict(dataframe, orient='index')
dfl=df.transpoze()
dfl.columns = ['comment', 'polarity', 'sentiment_type', 'subjectivity']
dfl.to_txt(r"C:\\Users\\Olga\\Desktop\\comment_sentiment_"+Movie_name+".txt", header=True, encoding='utf-8', index=False)
def youtube_video_url(options):
youtube = build("youtube", "v3", developerKey="AIzaSyDuV1ssRswKsW2uUjOWIyXWVh3sDDovBAw")
search_response = youtube.search().list(q=options.q, part="id,snippet", maxResults=options.max_results).execute()
for search_result in search_response.get("items", []):
if search_result["id"]["kind"] == "youtube#video":
videoId = search_result["id"]["videoId"]
print("videoId: " +str(videoId))
url="https://www.youtube.com/watch?v="+videoId
print(url)
return url
if __name__ == "__main__":
print("Enter the Movie Name : ")
Movie_name=str(input())
parser = argparse.ArgumentParser(description='youtube search')
parser.add_argument("--q", help="Search term", default=Movie_name+"Movie Trailer 2020")
parser.add_argument("--max-results", help="Max results", default=1)
args = parser.parse_args()
youtube_video_url=youtube_video_url(args)
scrape_comments(youtube_video_url,Movie_name)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question