1

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" class="style-scope ytd-playlist-panel-video-renderer">
</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 ?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

0

This should work

soup_obj.find_all("span", {"class":["style-scope", "ytd-playlist-panel-video-renderer"]})
Prophet
  • 32,350
  • 22
  • 54
  • 79
  • Unfortunately it did not work. –  Jan 03 '22 at 20:22
  • Are you sure the locator is correct? Can you share the link to that youtube and describe what are you trying to do there? – Prophet Jan 03 '22 at 20:43
  • Yes I do, we can test with any youtube playlist. Let's look at this one. soup_obj.find_all("a", {"class":["yt-simple-endpoint", "style-scope" , "ytd-playlist-panel-video-renderer"]}) for this link : https://www.youtube.com/playlist?list=PL4fGSI1pDJn4fmCoF1vKHLtivI0f9yHiF –  Jan 03 '22 at 21:34
  • OK, but you still didn't explain what exactly are you trying to do there? – Prophet Jan 03 '22 at 21:42
  • Yes I did and I told you it returned me 0 items. Which mean code is not able to fetch data from html. I have tried with Selenium and it works but with bs4 it didnt worked out. –  Jan 04 '22 at 13:41
0

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']")
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Hi, thank you for your help for detailed answer. The problem is I am trying to find it out via bs4 not with selenium. Because in Selenium you can do with multiple spaces. It is fine. Just dot between spaces work it out in all my projects but for bs4 dunno what to do –  Jan 03 '22 at 20:27