We don't really have any idea of how general purpose that the OP's function needs to be.
The OP's test input is very simple and doesn't demo anything complex.
It would be appropriate for the OP to post an extended test case.
I like your link+ and the discussion therein.
I certainly don't propose my simple code to be anything other than perhaps a "hack" to deal with one particular webpage.