在BeautifulSoup中使用多个条件

Md.*_*sin 6 python beautifulsoup python-2.7

我们使用此代码查找包含文本"Fiscal"的标签

soup.find(class_="label",text=re.compile("Fiscal"))
Run Code Online (Sandbox Code Playgroud)

如何在此处添加多个条件.

让我们说标签包含"财政"和"年".

或包含"Fiscal"而非"year"的标签

jfs*_*jfs 9

如果您发现标准有所不同并且可能会变得更复杂,那么您可以使用函数作为过滤器,例如:

让我们说标签包含"财政"和"年".

t = soup.find(class_="label", text=lambda s: "Fiscal" in s and "year" in s)
Run Code Online (Sandbox Code Playgroud)

或包含"Fiscal"而非"year"的标签

t = soup.find(class_="label", text=lambda s: "Fiscal" in s and "year" not in s)
Run Code Online (Sandbox Code Playgroud)

您也可以在这里使用正则表达式,但它可能不太可读.