要解决这个问题,我们可以使用正则表达式来匹配类名中的多个空格,并将其替换为单个空格。然后再使用Beautiful Soup的find_all方法来查找匹配的元素。
下面是一个示例代码:
import re
from bs4 import BeautifulSoup
# 假设html为你的HTML代码
html = """
Hello, World!
"""
# 使用正则表达式将类名中的多个空格替换为单个空格
html = re.sub(r'class="\s+', 'class="', html)
# 创建BeautifulSoup对象
soup = BeautifulSoup(html, 'html.parser')
# 使用find_all方法查找匹配的元素
elements = soup.find_all(class_="class1 class2 class3")
# 输出查找到的元素
print(elements)
运行上述代码,将会输出[
,表示成功找到了匹配的元素。
请注意,这只是一个示例代码,实际上你需要将html
变量替换为你的HTML代码,并根据你的需求调整正则表达式和查找的类名。