473,395 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

HTML cleaner?

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
7 2800
Probably you're looking for Beautiful Soup:
http://www.crummy.com/software/BeautifulSoup/

George

Jul 19 '05 #2
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Leif K-Brooks | last post by:
I'm writing a site with mod_python which will have, among other things, forums. I want to allow users to use some HTML (<em>, <strong>, <p>, etc.) on the forums, but I don't want to allow bad...
20
by: Al Moritz | last post by:
Hi all, I was always told that the conversion of Word files to HTML as done by Word itself sucks - you get a lot of unnecessary code that can influence the design on web browsers other than...
1
by: heren | last post by:
http://www.htmlcodecleaner.com-http.com/
1
by: fif3336 | last post by:
Hi everyone, I'm forwarding here a 100 euro bounty hunt I've started at drupal.org. It's about making a "word-html cleaner" module for the open source cms Drupal. All informations, including my...
2
by: Jake Barnes | last post by:
Using javascript closures to create singletons to ensure the survival of a reference to an HTML block when removeChild() may remove the last reference to the block and thus destory the block is...
7
by: Kenny M. | last post by:
Hi I want to write de content of a variable from my asp net code into the Title Bar of the Browser each time I need to switch between those world (HTML-ASPNET) I suffer cause I don’t have...
1
by: David Veeneman | last post by:
I'm writing an application that will manage a database of documents typed into the application by the user. Some documents will contain links to other documents. If the user clicks on a link, the...
3
by: sebzzz | last post by:
Hi, I'm doing a little script with the help of the BeautifulSoup HTML parser and uTidyLib (HTML Tidy warper for python). Essentially what it does is fetch all the html files in a given...
0
by: myprotein | last post by:
if "all applications disconnection" is anathor tigger of page cleaner? hi all I discovered that when all application disconnect from the database, db2 will "truncate" current active log file...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.