make a simple search function for homepage

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

    #1

    make a simple search function for homepage

    I want to add some simple search function for my homepage. It need to
    search through all the html files of my homepage (about 300 pages), and
    highlight the search words.

    I made some test with HTMLParser, it works but slow. So, my question is
    how can I improve its speed?

    from HTMLParser import HTMLParser

    class HightLightParse r(HTMLParser):
    def __init__(self, outfile, words):
    self.outfile = outfile
    self.words = words
    self.found = False
    HTMLParser.__in it__(self)

    def handle_starttag (self, tag, attrs):
    self.outfile.wr ite( self.get_startt ag_text( ) )

    def handle_endtag(s elf, tag):
    self.outfile.wr ite( "</%s>" % tag )

    def handle_data(sel f, data):
    for word in self.words:
    data = data.replace(wo rd, "<font color=red>%s</font>" % word)
    #highlight
    self.outfile.wr ite(data)

    class SearchParser(HT MLParser):
    def __init__(self, words):
    self.words = words
    self.found = False
    HTMLParser.__in it__(self)

    def handle_data(sel f, data):
    for word in self.words:
    if word in data: # search
    self.found = True


    words = ["the"]
    x = SearchParser(wo rds)
    data = file("input.htm ").read()
    x.feed(data)
    if x.found:
    y = HightLightParse r(file("output. htm", "w"),words)
    y.feed(data)

  • James Stroud

    #2
    Re: make a simple search function for homepage

    HYRY wrote:
    I want to add some simple search function for my homepage. It need to
    search through all the html files of my homepage (about 300 pages), and
    highlight the search words.
    >
    I made some test with HTMLParser, it works but slow. So, my question is
    how can I improve its speed?
    >
    from HTMLParser import HTMLParser
    >
    class HightLightParse r(HTMLParser):
    def __init__(self, outfile, words):
    self.outfile = outfile
    self.words = words
    self.found = False
    HTMLParser.__in it__(self)
    >
    def handle_starttag (self, tag, attrs):
    self.outfile.wr ite( self.get_startt ag_text( ) )
    >
    def handle_endtag(s elf, tag):
    self.outfile.wr ite( "</%s>" % tag )
    >
    def handle_data(sel f, data):
    for word in self.words:
    data = data.replace(wo rd, "<font color=red>%s</font>" % word)
    #highlight
    self.outfile.wr ite(data)
    >
    class SearchParser(HT MLParser):
    def __init__(self, words):
    self.words = words
    self.found = False
    HTMLParser.__in it__(self)
    >
    def handle_data(sel f, data):
    for word in self.words:
    if word in data: # search
    self.found = True
    >
    >
    words = ["the"]
    x = SearchParser(wo rds)
    data = file("input.htm ").read()
    x.feed(data)
    if x.found:
    y = HightLightParse r(file("output. htm", "w"),words)
    y.feed(data)
    >
    google "google".

    Seriously though, perhaps you may want to index your pages first. Maybe
    checkout divmod (http://divmod.org/trac/wiki/DivmodXapwrap).

    James

    Comment

    Working...