Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Massage with BeautifulSoup or clean with Regex

Could someone tell me whats a better way to clean up bad HTML so BeautifulSoup can handle it - should one use the massage methods of BeautifulSoup or clean it up using regular expressions?

like image 764
Mridang Agarwalla Avatar asked Jun 17 '10 11:06

Mridang Agarwalla


1 Answers

Thought I should reword my answer.

The built-in massages are good for light damage (extra whitespace, no closing slashes, etc). I would certainly try and get away with these before getting any more involved.

You can pass in your own massages and I would suggest you extend the default set:

import copy, re

myMassage = [(re.compile('<!-([^-])'), lambda match: '<!--' + match.group(1))]
myNewMassage = copy.copy(BeautifulSoup.MARKUP_MASSAGE)
myNewMassage.extend(myMassage)

BeautifulSoup(badString, markupMassage=myNewMassage)
# Foo<!--This comment is malformed.-->Bar<br />Baz

You're probably better off doing it this way as it all goes into one parsing pot, gaining BeautifulSoups optimisations... Although the runtime performance is probably pretty similar.

like image 118
Oli Avatar answered Oct 04 '22 10:10

Oli