I am using following code to match all div that have CSS class \"ad_item\".
soup.find_all(\'div\',class_=\"ad_item\")
problem that I have i
You can pass a lambda functions to find
and find_all
methods.
soup.find_all(lambda x:
x.name == 'div' and
'ad_item' in x.get('class', []) and
not 'ad_ex_item' in x['class']
)
The x.get('class', [])
will avoid KeyError
exceptions for div
tags without class
attribute.
If you need to exclude more than only one class you can substitute the last condition with:
not any(c in x['class'] for c in {'ad_ex_item', 'another_class'})
And if you want to exclude exactly some classes you can use:
not all(c in x['class'] for c in {'ad_ex_item', 'another_class'})