你可以用漂亮的汤中的classname搜索吗?

时间:2014-12-30 19:06:33

标签: python beautifulsoup

在BeautifulSoup中,您可以使用soup.find_all进行搜索。例如,我使用

搜索了一个页面
soup.find_all("tr", "cat-list-row1")

显然,这带来了每个名为cat-list-row1的tr类。我想知道是否有可能在整个页面中搜索任何名为“cat-list-row1”的类,而不是将其限制为元素为“tr”的类。

1 个答案:

答案 0 :(得分:4)

Multiple ways

  • 使用class_参数(class不能使用,它是Python中的保留关键字):

    soup.find_all(class_="cat-list-row1")
    
  • 使用attrs字典

    soup.find_all(attrs={"class": "cat-list-row1"})
    
  • 使用CSS selector

    soup.select('.cat-list-row1')
    

请注意BeautifulSoup可以轻松应用"多值属性"概念:

  

请记住,单个标记的“类”可以有多个值   属性。当您搜索与某个CSS类匹配的标记时,   你正在匹配任何CSS类。