Jump to content

Finding a word in a webpage


ajosif
 Share

Recommended Posts

Hi,I want to make a simple program that will search for one word inside the webpage something like:search("Geranium", "www.webpage.com")if found() write("www.123.com", "Geranium")Was told that cannot be don in javascript, but some more advanced language like Java.I don't need to run a website just execute a progam on my computer that will search the webpages.What is the simplest/easiest language that can do this? Java is too complicated for me.Thank you

Link to comment
Share on other sites

[edit: you might want to read the next message before bothering with this one.]I've never had the need to do anything like this, but I'm sure it can be done in javascript. If you're accustomed to recursive functions, you're halfway home.What you'll need to do is dig through the childnodes of the document body, and their nodes, etc., and test each one to see if it's a text node. If it is, do some sort of match or equality comparison. Then move on till you've hit every branch on the tree.I believe there are examples out there of "tree-walker" routines.Someone may even have a getTextNodes() function out there or a getNodesByType(), but these aren't built into the DOM, even though it seems obvious that they should be. If you're lucky, now that you know what to look for, you might find something. But it's writable with pretty basic programming skills.

Link to comment
Share on other sites

You can use Java, PHP, C#, VB, perl, python, etc.. It could be either a web application or a desktop application. The process that needs to happen is that you tell your application the URL that you want to fetch and then it creates an HTTP Request and sends it off to that URL and retrieves the response. Then you parse through the text of the response looking for your search string.I'm partial to C# and have done something like this a number of times using either System.Net.WebClient, System.Web.HttpRequest, or System.Net.Sockets.TcpSocket.If this is for a web page, you can start looking at how people build AJAX proxies:http://www.google.com/search?q=ajax+proxy

Link to comment
Share on other sites

Hi,Yes, I want to search some other's webpages, there will be a listo of different web pages.Looks like there's no simple way to do that, have to learn more programming.In a web page I can manually view the source and search it, or just do Ctrl+F type the word and will highlight it if there.Thank you anyway, I was hoping it can be done in just a few lines of code.

Link to comment
Share on other sites

Well, it's fairly trivial once you know the languages, for example in PHP you might do it like this:

<?php$sites = array( 'http://site1.com/index.html', 'http://site2.com/index.html' );foreach($sites as $site)   if(strpos(file_get_contents($site),'SEARCHWORD') !== false)	  echo $site,' contains the term.',"\n";?>

That will do pretty much what you are saying, of course you need a PHP parser to run it.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...