查找包含两个指定属性值的标记的多个表达式的\u all

2024-10-03 11:12:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在努力从属性值为h5和ls3的HTML文件中提取所有标记,但我不确定如何格式化搜索,以便它不会返回h5的所有实例和ls3的所有实例

regex = re.compile('h5')
re2 = re.compile('ls3')

ly = []


for EachPart in soup.find_all("div", {"class": regex}):
    ly.append(EachPart)

这目前只搜索h5(正如预期的那样),但是如何将find_all搜索格式化为与regex一起搜索re2?实际上,下面我需要拉标签,有h5和ls3,而不是那些专门的h5

HTML标记示例:

<div class="t m0 x42 h5 y18b ff2 fs2 fc0 sc0 ls0 ws0">total </div>

<div class="t m0 xbd h5 y18b ff2 fs2 fc0 sc0 ls0 ws0"> </div>

<div class="t m0 x79 h5 y18b ff2 fs2 fc0 sc0 ls3 ws0">£m<span class="ls0"> </span></div>

<div class="t m0 x0 h5 y628 ff2 fs2 fc0 sc0 ls3 ws0">17.<span class="ls0"> <span class="_ _4"></span>Website policy<span class="_ _0"></span> </span></div>

Tags: divhtmlclassregexh5spanm0fc0