如何使用BeautifulSoup和BS4处理多个类



我正在尝试编写youtube scraper,作为任务的一部分,我需要在bs4上处理多个类。

HTML看起来像

<span id="video-title" class="style-scope ytd-playlist-panel-video-renderer">
</span>

我的目标是使用class属性来获得所有50种不同的音乐并与它们一起工作。我已经试过了,但没有任何回报。

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

我还尝试了Selenium风格(而不是类之间的空格pass dot(.(

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

有人知道吗?

这应该能在中工作

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

使用Selenium不能在中发送多个类名

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

如果您的目标是只使用class属性,那么您只需要传递一个classname,并且您可以使用以下定位器策略之一:

  • 使用类名作为style-scope:

    elements = driver.find_elements(By.CLASS_NAME, "style-scope")
    
  • 使用类名作为style-scope:

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

要传递两个类名,可以使用以下定位器策略之一:

  • 使用CSS_SELECTOR

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

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

最新更新