How to find elements by class

后端 未结 17 1637
有刺的猬
有刺的猬 2020-11-22 08:33

I\'m having trouble parsing HTML elements with \"class\" attribute using Beautifulsoup. The code looks like this

soup = BeautifulSoup(sdata)
mydivs = soup.fi         


        
17条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 08:52

    CSS selectors

    single class first match

    soup.select_one('.stylelistrow')
    

    list of matches

    soup.select('.stylelistrow')
    

    compound class (i.e. AND another class)

    soup.select_one('.stylelistrow.otherclassname')
    soup.select('.stylelistrow.otherclassname')
    

    Spaces in compound class names e.g. class = stylelistrow otherclassname are replaced with ".". You can continue to add classes.

    list of classes (OR - match whichever present

    soup.select_one('.stylelistrow, .otherclassname')
    soup.select('.stylelistrow, .otherclassname')
    

    bs4 4.7.1 +

    Specific class whose innerText contains a string

    soup.select_one('.stylelistrow:contains("some string")')
    soup.select('.stylelistrow:contains("some string")')
    

    Specific class which has a certain child element e.g. a tag

    soup.select_one('.stylelistrow:has(a)')
    soup.select('.stylelistrow:has(a)')
    

提交回复
热议问题