I'm using the BeautifulSoup module in Python 3.10 to get HTML data off a web page.
The way this web page is structured, there's a bunch of <h1> tags which section the web page. Some of these will look like <h1 class="title"><img attributes/> Text</h1>, and some will lack the img element but are otherwise identical.
Suppose I start with a string called name and a BeautifulSoup object called soup. This BeautifulSoup object contains several <h1> tags as described above, each of which is followed by more HTML code. Suppose further that no two <h1> elements contain identical text.
I'd like to compile a function which does the following:

Searches the BeautifulSoup object for a <h1> element which contains a string that, excluding the <img> content, exactly matches the input string name.
If it's not the last <h1> tag in the BeautifulSoup object, return everything from that <h1> tag until the next <h1> tag. The latter tag shouldn't be included in the return, but the former tag can be optionally included or excluded. If it is the last <h1> tag, return everything from that tag to the end of the object.

I'm only just learning BeautifulSoup. I know how to use .find() or .find_all() to track down which <h1> tag matches, but I don't know how to return all the following blocks as well.
Actually, when you crawl data using BeautifulSoup. All HTML tags will be written down. Just use a loop to find that checks for your image's existence or not.
BeautifulSoup in Python: Get series of tags where first exactly matches input