HTML cleaner?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ivan Voras

    #1

    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.

  • George Sakkis

    #2
    Re: HTML cleaner?

    Probably you're looking for Beautiful Soup:


    George

    Comment

    • Fuzzyman

      #3
      Re: HTML cleaner?

      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(inte xt, allowed_tags=al lowed_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(inte xt) #
      out_html = []
      skip = None
      unclosed = []
      for entry in html:
      if isinstance(entr y, 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.remov e(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_htm l)


      ###############

      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

      Comment

      • M.-A. Lemburg

        #4
        Re: HTML cleaner?

        Ivan Voras wrote:[color=blue]
        > 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.[/color]

        Not true: mxTidy integrates tidy as C lib. It's not an interface
        to the command line tool.
        [color=blue]
        > What I'm searching is something that will accept a list of allowed tags
        > and/or attributes and strip the rest from HTML string.[/color]

        --
        Marc-Andre Lemburg
        eGenix.com

        Professional Python Services directly from the Source (#1, Apr 25 2005)[color=blue][color=green][color=darkred]
        >>> Python/Zope Consulting and Support ... http://www.egenix.com/
        >>> mxODBC.Zope.Dat abase.Adapter ... http://zope.egenix.com/
        >>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/[/color][/color][/color]
        _______________ _______________ _______________ _______________ ____________

        ::: Try mxODBC.Zope.DA for Windows,Linux,S olaris,FreeBSD for free ! ::::

        Comment

        • Leif K-Brooks

          #5
          Re: HTML cleaner?

          Ivan Voras wrote:[color=blue]
          > 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.[/color]

          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.

          Comment

          • Ivan Voras

            #6
            Re: HTML cleaner?

            M.-A. Lemburg wrote:
            [color=blue]
            > Not true: mxTidy integrates tidy as C lib. It's not an interface
            > to the command line tool.[/color]

            Thanks, I'll look at it again!

            Comment

            • Walter Dörwald

              #7
              Re: HTML cleaner?

              Ivan Voras wrote:[color=blue]
              > M.-A. Lemburg wrote:
              >[color=green]
              >> Not true: mxTidy integrates tidy as C lib. It's not an interface
              >> to the command line tool.[/color]
              >
              > Thanks, I'll look at it again![/color]

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

              Bye,
              Walter Dörwald

              Comment

              • Terry Hancock

                #8
                Re: HTML cleaner?

                On Sunday 24 April 2005 06:25 pm, Ivan Voras wrote:[color=blue]
                > 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.[/color]

                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.



                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 anansispacework s.com )
                Anansi Spaceworks http://www.anansispaceworks.com

                Comment

                Working...