We use this code find tags containing the text "Fiscal"
soup.find(class_="label",text=re.compile("Fiscal"))
How do I put multiple conditions in here.
Lets say tags containing "Fiscal" and "year" both.
Or tags containing "Fiscal" and NOT "year"
To find multiple tags, you can use the , CSS selector, where you can specify multiple tags separated by a comma , . To use a CSS selector, use the . select_one() method instead of . find() , or .
To find elements by class in Beautiful Soup use the find_all(~) or select(~) method.
Using CSS selectors to locate elements in BeautifulSoupUse select() method to find multiple elements and select_one() to find a single element.
If you see that the criteria vary and they might get more complex then you could use a function as a filter e.g.:
Lets say tags containing "Fiscal" and "year" both.
t = soup.find(class_="label", text=lambda s: "Fiscal" in s and "year" in s)
Or tags containing "Fiscal" and NOT "year"
t = soup.find(class_="label", text=lambda s: "Fiscal" in s and "year" not in s)
You could also use a regex here but it might be less readable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With