Beautifulsoup multiple class selector

前端 未结 5 1967
青春惊慌失措
青春惊慌失措 2020-12-05 07:20

I want to select all the divs which have BOTH A and B as class attributes.

The following selection

soup.findAll(\'div\', class_=[\'A\', \'B\'])
         


        
5条回答
  •  佛祖请我去吃肉
    2020-12-05 08:07

    for latest BeautifulSoup, you can use regex to search class

    code:

    import re
    from bs4 import BeautifulSoup
    
    multipleClassHtml = """
    
    only A and B
    class contain space
    except A and B contain other class
    only A
    only B
    no A B
    """ soup = BeautifulSoup(multipleClassHtml, 'html.parser') bothABClassP = re.compile("A\s+B", re.I) foundAllAB = soup.find_all("div", attrs={"class": bothABClassP}) print("foundAllAB=%s" % foundAllAB)

    output:

    foundAllAB=[
    only A and B
    ,
    class contain space
    ,
    except A and B contain other class
    ]

    vscode debug bs4

提交回复
热议问题