Stripping scripts from HTML with regular expressions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michel Bouwmans

    Stripping scripts from HTML with regular expressions

    Hey everyone,

    I'm trying to strip all script-blocks from a HTML-file using regex.

    I tried the following in Python:

    testfile = open('testfile' )
    testhtml = testfile.read()
    regex = re.compile('<sc ript\b[^>]*>(.*?)</script>', re.DOTALL)
    result = regex.sub('', blaat)
    print result

    This strips far more away then just the script-blocks. Am I missing
    something from the regex-implementation from Python or am I doing something
    else wrong?

    greetz
    MFB
  • Reedick, Andrew

    #2
    RE: Stripping scripts from HTML with regular expressions


    -----Original Message-----
    From: python-list-bounces+jr9445= att.com@python. org [mailto:python-
    list-bounces+jr9445= att.com@python. org] On Behalf Of Michel Bouwmans
    Sent: Wednesday, April 09, 2008 3:38 PM
    To: python-list@python.org
    Subject: Stripping scripts from HTML with regular expressions

    Hey everyone,

    I'm trying to strip all script-blocks from a HTML-file using regex.

    I tried the following in Python:

    testfile = open('testfile' )
    testhtml = testfile.read()
    regex = re.compile('<sc ript\b[^>]*>(.*?)</script>', re.DOTALL)
    result = regex.sub('', blaat)
    print result

    This strips far more away then just the script-blocks. Am I missing
    something from the regex-implementation from Python or am I doing
    something
    else wrong?
    [Insert obligatory comment about using a html specific parser
    (HTMLParser) instead of regexes.]

    Actually your regex didn't appear to strip anything. You probably saw
    stuff disappear because blaat != testhtml:
    testhtml = testfile.read()
    result = regex.sub('', blaat)


    Try this:

    import re

    testfile = open('a.html')
    testhtml = testfile.read()
    regex = re.compile('<sc ript\s+.*?>(.*? )</script>', re.DOTALL)
    result = regex.sub('',te sthtml)

    print result




    Comment

    • Stefan Behnel

      #3
      Re: Stripping scripts from HTML with regular expressions

      Michel Bouwmans wrote:
      I'm trying to strip all script-blocks from a HTML-file using regex.
      You might want to take a look at lxml.html instead, which comes with an HTML
      cleaner module:



      Stefan

      Comment

      • Nikita the Spider

        #4
        Re: Stripping scripts from HTML with regular expressions

        In article <mailman.161.12 07771905.17997. python-list@python.org >,
        "Reedick, Andrew" <jr9445@ATT.COM wrote:
        -----Original Message-----
        From: python-list-bounces+jr9445= att.com@python. org [mailto:python-
        list-bounces+jr9445= att.com@python. org] On Behalf Of Michel Bouwmans
        Sent: Wednesday, April 09, 2008 3:38 PM
        To: python-list@python.org
        Subject: Stripping scripts from HTML with regular expressions

        Hey everyone,

        I'm trying to strip all script-blocks from a HTML-file using regex.
        >
        [Insert obligatory comment about using a html specific parser
        (HTMLParser) instead of regexes.]
        Yah, seconded. To the OP - use BeautifulSoup or HtmlData unless you like
        to reinvent wheels.

        --
        Philip

        Whole-site HTML validation, link checking and more

        Comment

        Working...