473,772 Members | 2,292 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)

Oct 31 '06 #1
1 2769
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
Oct 31 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
12754
by: ajay | last post by:
How to make a web page getting refreshed after a given time interval automatically. HTML Code plz. Tx Ajay
4
337
by: timothy ma and constance lee | last post by:
i am new to web page and start to write a simple homepage using pure html and javascript because it is a static page however, i want to ask that when i click link, can i pass the parameter to another page, by using javascript?
67
6050
by: Sandy.Pittendrigh | last post by:
Here's a question I don't know the answer to: I have a friend who makes very expensive, hand-made bamboo flyrods. He's widely recognized (in the fishing industry) as one of the 3-5 'best' rod makers in the world. He gets (sic) close to $5000 per custom made flyrod. A surprising number of people buy these fishing rods and never use them....they buy them as art-like investments. He is, after all, the best there is. But if you search on...
10
5798
by: DataBard007 | last post by:
Hello Access Gurus: I use Win98SE and Access97. I just built a simple Access97 application which holds all contact information for my personal contacts, such as first name, last name, address, city, state, etc. When the user wants to search for a particular record, he does two things: 1. On the form is a text box on which he enters the text he is searching for.
1
1819
by: Robert | last post by:
Vb.Net Make dll that contain one function. Help Please. I would like to call a function from different applications. I think i have to make a dll. I have Visual Basic.net 2003 Standard Edition, that teorically does not make dll, but pratically does. I search vbc.exe and copy it to a new folder, in which i have the
0
1008
by: Bell, Kevin | last post by:
I know I can use urllib2 to get at a website given urllib2.urlopen(url) but I'm unsure how to then go through all pages that are linked to it, but still in the domain. If I want to search through the entire python website give the homepage, how would I go about it? I don't reinvent the wheel if someones already written a module for parsing out the links and drilling through them! TIA KEV
1
7292
by: xahlee | last post by:
Elisp Tutorial: Make Google Earth Xah Lee, 2006-12 This page shows a example of writing a emacs lisp function that creates a Google Earth file, and creates a link to the file, as well a link to Google Map. If you don't know elisp, first take a gander at Elisp Basics. I often write travelogs on my website. If i traveled to Las Vegas, then
3
1023
by: flit | last post by:
Hello All, I was discussing with a friend and we get in a hot debate. We have homepage that act like a name list. The user goes there: - Choose the building -- Department and the user receive an html table with all names .
1
47485
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click on a link and after a moment or two a file download dialog box pops-up in your web browser and prompts you for some instructions, such as “open” or “save“. I’m going to show you how to do that using a perl script. What You Need Any recent...
0
9621
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10264
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10106
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9914
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8937
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7461
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.