@OlyaZZZzzzzz8

Пытаюсь спарсить и классифицировать комментарии с youtube, вот такие ошибки выдает, может кто знает?

Вот код:
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)

Ошибки которые он выдает:
Warning (from warnings module):
File "C:\Users\Olga\Desktop\1.py", line 44
sort= driver.find_element_by_xpath("""//*[@id="icon_label"]""")
DeprecationWarning: find_element_by_xpath is deprecated. Please use find_element(by=By.XPATH, value=xpath) instead
Traceback (most recent call last):
File "C:\Users\Olga\Desktop\1.py", line 118, in
scrape_comments(youtube_video_url,Movie_name)
File "C:\Users\Olga\Desktop\1.py", line 44, in scrape_comments
sort= driver.find_element_by_xpath("""//*[@id="icon_label"]""")
File "C:\Users\Olga\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 521, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\Olga\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1248, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\Olga\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 425, in execute
self.error_handler.check_response(response)
File "C:\Users\Olga\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="icon_label"]"}
(Session info: chrome=100.0.4896.60)
Stacktrace:
Backtrace:
Ordinal0 [0x00449943+2595139]
Ordinal0 [0x003DC9F1+2148849]
Ordinal0 [0x002D4528+1066280]
Ordinal0 [0x00300FD4+1249236]
Ordinal0 [0x003011CB+1249739]
Ordinal0 [0x0032D812+1431570]
Ordinal0 [0x0031BA34+1358388]
Ordinal0 [0x0032BAF2+1424114]
Ordinal0 [0x0031B806+1357830]
Ordinal0 [0x002F6086+1204358]
Ordinal0 [0x002F6F96+1208214]
GetHandleVerifier [0x005EB232+1658114]
GetHandleVerifier [0x006A312C+2411516]
GetHandleVerifier [0x004DF261+560433]
GetHandleVerifier [0x004DE366+556598]
Ordinal0 [0x003E286B+2173035]
Ordinal0 [0x003E75F8+2192888]
  • Вопрос задан
  • 104 просмотра
Решения вопроса 1
@AVKor
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="icon_label"]"}
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы