0

I can't seem to find out how to search for classes that contains a specific string i.e. searching for the class "Header" would return both the class "This-Header" and "Header-that".

Anyone got a workable solution to this?

no nein
  • 631
  • 3
  • 10
  • 27

1 Answers1

2

You can pass callables and regular expressions as the class_ keyword argument:

import re

soup.find('a', class_=re.compile(r'Header'))

Or:

soup.find('a', class_=lambda c: 'Header' in c)

Both of these will match <a class="Test-Headers"></a>, so you'll have to adjust them if that's not behavior you want.

Blender
  • 289,723
  • 53
  • 439
  • 496