By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,587 Members | 1,575 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,587 IT Pros & Developers. It's quick & easy.

HTML cleaner?

P: n/a
Is there a HTML clean/tidy library or module written in pure python? I
found mxTidy, but it's a interface to command-line tool.

What I'm searching is something that will accept a list of allowed tags
and/or attributes and strip the rest from HTML string.

Jul 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Probably you're looking for Beautiful Soup:
http://www.crummy.com/software/BeautifulSoup/

George

Jul 19 '05 #2

P: n/a
I *just* wrote something that does this. It uses the htmldata module -
you can find that using pypi. It only allows a specific set of html
tags and attempts to close tags not closed. :

from htmldata import tagextract, tagjoin
allowed_tags = ['br', 'b', 'strong', 'em', 'i', 'u', 'tt', 'a', 'big',
'small',
'h2', 'h3', 'h4', 'strike', 'sub', 'sup', 'samp', 's',
'code', 'ins',
'br/',
]
def htmlfilter(intext, allowed_tags=allowed_tags):
"""Given a text entry as input, check it only contains allowed html.

It returns the text with banned html removed.
Uses two functiosn from htmldata :
from htmldata import tagextract, tagjoin
allowed_tags is the list of tags that are allowed.
"""
html = tagextract(intext) #
out_html = []
skip = None
unclosed = []
for entry in html:
if isinstance(entry, basestring):
if skip is not None:
continue
out_html.append(entry)
else:
tag = entry[0]
if skip is not None:
if tag.startswith('/') and tag[1:] == skip:
skip = None
continue
otag = tag
if tag.startswith('/'):
otag = tag[1:]
if otag in allowed_tags:
if tag.startswith('/'):
if otag in unclosed:
unlclosed.remove(otag)
else: # bad html
continue
elif tag not in ['br', '/br', 'hr', '/hr', 'img',
'/img']: # XXXX hardwired - what if we need to add to this ?
unclosed.append(tag)
out_html.append(entry)
continue
if not tag.startswith('/'):
skip = tag
for tag in unclosed:
out_html.append(('/%s' % (tag,), {})) # close any unclosed
tags
return tagjoin(out_html)
###############

I've used it to allow a few html tags to appear in my guestbook
entries. It's not very sophisticated because complex tags like 'div'
and tables aren't allowed.

Best regards,

Fuzzy
http://www.voidspace.org.uk/python

Jul 19 '05 #3

P: n/a
Ivan Voras wrote:
Is there a HTML clean/tidy library or module written in pure python? I
found mxTidy, but it's a interface to command-line tool.
Not true: mxTidy integrates tidy as C lib. It's not an interface
to the command line tool.
What I'm searching is something that will accept a list of allowed tags
and/or attributes and strip the rest from HTML string.


--
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source (#1, Apr 25 2005)
Python/Zope Consulting and Support ... http://www.egenix.com/
mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/

__________________________________________________ ______________________

::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,FreeBSD for free ! ::::
Jul 19 '05 #4

P: n/a
Ivan Voras wrote:
Is there a HTML clean/tidy library or module written in pure python? I
found mxTidy, but it's a interface to command-line tool.

What I'm searching is something that will accept a list of allowed tags
and/or attributes and strip the rest from HTML string.


Here's a module I wrote to do something along the lines of what you
want: <http://ecritters.biz/limithtml.py>. Unfortunately, it requires
the HTML to be relatively well-formed (e.g. it doesn't like things like
"<i><b>foo</i></b>"), so I feed the HTML into uTidyLib (another
interface to HTML Tidy) first. I'm not sure why you don't want to use
Tidy, but if you do change your mind, you should be able to use my
module alongside Tidy to limit the HTML elements and attributes which
will be accepted.
Jul 19 '05 #5

P: n/a
M.-A. Lemburg wrote:
Not true: mxTidy integrates tidy as C lib. It's not an interface
to the command line tool.


Thanks, I'll look at it again!
Jul 19 '05 #6

P: n/a
Ivan Voras wrote:
M.-A. Lemburg wrote:
Not true: mxTidy integrates tidy as C lib. It's not an interface
to the command line tool.


Thanks, I'll look at it again!


Another option might be the HTML parser (libxml2.htmlReadMemory()) from
libxml2 (http://www.xmlsoft.org)

Bye,
Walter Dörwald
Jul 19 '05 #7

P: n/a
On Sunday 24 April 2005 06:25 pm, Ivan Voras wrote:
Is there a HTML clean/tidy library or module written in pure python? I
found mxTidy, but it's a interface to command-line tool.

What I'm searching is something that will accept a list of allowed tags
and/or attributes and strip the rest from HTML string.


I'm using stripogram for this. It uses a whitelist approach, where you
tell it what tags to accept. It also has a function for getting text only.

http://www.zope.org/Members/chrisw/StripOGram/

It's very useful in Zope, but is actually an independent pure-python
module (you don't need Zope to use it). Also very small.

--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks http://www.anansispaceworks.com

Jul 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.