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\'])
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]
