Home > Back-end >  How to work with multiple classes using BeautifulSoup & BS4
How to work with multiple classes using BeautifulSoup & BS4

Time:01-04

I am trying to write youtube scraper and as part of my task I need to work with multiple classes at bs4.

HTML looks like

<span id="video-title" >
</span>

My aim to use class attribute to get all 50 different music and work with them. I have tried like that and it returns me nothing.

soup_obj.find_all("span", {"class":"style-scope ytd-playlist-panel-video-renderer"})

and I also tried as Selenium style (instead of spaces between class pass dot(.)

soup_obj.find_all("span", {"class":"style-scope.ytd-playlist-panel-video-renderer"})

Does anyone have idea about it ?

CodePudding user response:

This should work

soup_obj.find_all("span", {"class":["style-scope", "ytd-playlist-panel-video-renderer"]})

CodePudding user response:

Using Selenium you can't send multiple classnames within:

driver.find_elements(By.CLASS_NAME, "classname")

If your aim is to use only class attribute then you need to pass only a single classname and you can use either of the following Locator Strategies:

  • Using classname as style-scope:

    elements = driver.find_elements(By.CLASS_NAME, "style-scope")
    
  • Using classname as style-scope:

    elements = driver.find_elements(By.CLASS_NAME, "ytd-playlist-panel-video-renderer")
    

To pass both the classnames you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    elements = driver.find_elements(By.CSS_SELECTOR, "span.style-scope.ytd-playlist-panel-video-renderer")
    
  • Using XPATH:

    elements = driver.find_elements(By.XPATH, "//span[@class='style-scope ytd-playlist-panel-video-renderer']")
    
  • Related