#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    10
    Rep Power
    0

    Help me please??


    can anyone help me how can I extract text from webpage and count occurence of the words and the result should display in table and user should select if the order is alphapatic or frequencey please help me ??
    thanks in advance
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,165
    Rep Power
    184
    This will let you define the word your looking for and display results accordingly:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    
    <script>
    
    var count = 0;
    
    var cached="no";
    
    function counter(word,counTitle,wordTitle)
    {
    
    content = document.getElementsByTagName("body")[0].innerHTML.split(" ");
    
    for (i=0;i<content.length;i++) {
    content[i] = content[i].replace("\n"," ");
    if (content[i].match(word)) {
    count++;
    }
    }
    
    var countHeadingRow = document.createElement("tr");
    var HeadingTD = document.createElement("td");
    var HeadingTD2 = document.createElement("td");
    var newTR = document.createElement("tr");
    var newTD = document.createElement("td");
    var newTD2 = document.createElement("td");
    
    HeadingTD.innerHTML = counTitle;
    HeadingTD2.innerHTML = wordTitle;
    newTD.innerHTML = count;
    newTD2.innerHTML = word;
    
    // create heading
    
    if (cached != "yes") {
    document.getElementById("wordData").appendChild(countHeadingRow);
    document.getElementById("wordData").getElementsByTagName("tr")[0].appendChild(HeadingTD);
    document.getElementById("wordData").getElementsByTagName("tr")[0].appendChild(HeadingTD2);
    cached="yes";
    }
    
    // create new data rows for each word/quanity
    
    document.getElementById("wordData").appendChild(newTR);
    var maxRows = document.getElementById("wordData").getElementsByTagName("tr").length - 1;
    document.getElementById("wordData").getElementsByTagName("tr")[maxRows].appendChild(newTD);
    document.getElementById("wordData").getElementsByTagName("tr")[maxRows].appendChild(newTD2);
    
    // reset all
    
    count = 0;
    content = [];
    
    }
    
    document.onreadystatechange = function() {
    
    if (document.readyState=="complete") {
    
    // add "counter" function and define parameters, too find case sensitive words & word quanities
    
    counter("rock","Amount Displayed","Word Displayed");
    counter("wanna","Amount Displayed","Word Displayed");
    counter("Turn","Amount Displayed","Word Displayed");
    counter("go","Amount Displayed","Word Displayed");
    counter("Go","Amount Displayed","Word Displayed");
    counter("No","Amount Displayed","Word Displayed");
    
    }
    
    }
    
    </script>
    
    
    </head>
    
    <body>
    
    <!-- demo content - begin -->
    
    <pre style="text-align:center">
    I wanna rock! rock!
    I wanna rock! rock!
    I want to rock rock!
    I wanna rock! rock!
    
    Turn it down you say,
    Well all I got to say to you is time again I say, "No!"
    No! No, No, No, No, No!
    Tell me not to play
    Well, all I got to say to you when you tell me not to play,
    I say, "No!"
    No! No, No, No, No, No!
    So, if you ask me why I like the way I play it
    There's only one thing I can say to you
    
    I wanna rock! rock!
    I wanna rock! rock!
    I want to rock rock!
    I wanna rock! rock!
    
    There's a feelin' that
    I get from nothin' else and there ain't nothin' in the world
    That makes me go!
    Go! Go, go, go, go, go!
    Turn the power up
    I've waited for so long so I could hear my favorite song so,
    Let's go!
    Go! Go, go, go, go, go!
    When it's like this I feel the music shootin' through me
    There's nothin' else that I would rather do
    
    I wanna rock! rock!
    I wanna rock! rock!
    I want to rock rock!
    I wanna rock! rock!
    </pre>
    
    <!-- demo content - end -->
    
    
    <br/>
    <br/>
    <br/>
    
    <table id="wordData" border="1" width="100%"></table>
    
    </body>
    
    </html>
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    10
    Rep Power
    0
    Thanky fo helping but the count should be from another web page so it let user to type the url of the page to count and the result should be in a table .... How can I solve this
  6. #4
  7. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,165
    Rep Power
    184
    You could go about doing this a couple different ways, but the main idea is..., that your going to have to scrape the page's content and get text/responseText back into the "content" variable. You can set-up a server side page to get the content's of a page once it has been queried. A basic example in my chosen server side language would be:

    [PHP=scrapper.php]<?php
    header("Content-Type:application/javascript");
    $defineURL = $_GET["url"];
    $contents = file_get_contents("".$defineURL."");
    if (isset($defineURL)) {
    echo "var content=\"$content\";\n\n";
    }
    else {
    echo "var content=\"No Content Found!\";\n\n";
    }
    ?>[/PHP]

    Then add this to your page (remove the content variable declaration; that I originally had in the code.... ie. "var content;"); with an external javascript link... ie. "<script type="text/javascript" src="scrapper.php"></script>" or you could get the responseText with AJAX and insert it into the "content" variable.

    Or... an alternative would be to use and incorporate the YQL Module for YUI; which is a screen scraper plugin/API from Yahoo's YUI 3.0 Framework.

    Or... you could screen scrape the page and serve it as plain text or html and include it in an iframe (within the page the search is occurring from) and then have the "content" variable store the innerHTML of the contentWindow || contentDocument of the iframe.

    Or... you could just do away with the client side aspect (javascript) and do this completely server side. In my opinion, this option... would be the best way to go.

    So... yeah, you have several ways you could do this. Do some online searching for "screen scraping" and/or "server side screen scraping" and/or "client side screen scraping" and/or "ajax screen scraping" and/or "javascript screen scraping". Those are all good places to start from... good luck.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    10
    Rep Power
    0
    I have put the code to get the url from the user to count
    however i do not know what should I have to put in the counter()??? to diblay the result
    here is my code
    [code]
    <!DOCTYPE html>
    <html>
    <head>

    <script>

    var count = 0;

    var cached="no";

    function counter(word,counTitle,wordTitle)
    {

    content = document.getElementsByTagName("body")[0].innerHTML.split(" ");

    for (i=0;i<content.length;i++) {
    content[i] = content[i].replace("\n"," ");
    if (content[i].match(word)) {
    count++;
    }
    }

    var countHeadingRow = document.createElement("tr");
    var HeadingTD = document.createElement("td");
    var HeadingTD2 = document.createElement("td");
    var newTR = document.createElement("tr");
    var newTD = document.createElement("td");
    var newTD2 = document.createElement("td");

    HeadingTD.innerHTML = counTitle;
    HeadingTD2.innerHTML = wordTitle;
    newTD.innerHTML = count;
    newTD2.innerHTML = word;

    // create heading

    if (cached != "yes") {
    document.getElementById("wordData").appendChild(countHeadingRow);
    document.getElementById("wordData").getElementsByTagName("tr")[0].appendChild(HeadingTD);
    document.getElementById("wordData").getElementsByTagName("tr")[0].appendChild(HeadingTD2);
    cached="yes";
    }

    // create new data rows for each word/quanity

    document.getElementById("wordData").appendChild(newTR);
    var maxRows = document.getElementById("wordData").getElementsByTagName("tr").length - 1;
    document.getElementById("wordData").getElementsByTagName("tr")[maxRows].appendChild(newTD);
    document.getElementById("wordData").getElementsByTagName("tr")[maxRows].appendChild(newTD2);

    // reset all

    count = 0;
    content = [];

    }

    document.onreadystatechange = function() {

    if (document.readyState=="complete") {

    // add "counter" function and define parameters, too find case sensitive words & word quanities


    }

    }

    </script>


    </head>

    <body>
    <p>Web Page URL : <input id = "url" name="url" size=25 type="Text"/></p>
    <a href="javascript:coutner(document.getElementById('url').value)"> count word in document document</a></br>
    <table id="wordData" border="1" width="20"></table>

    </body>

    </html>[code]
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    please read the replies you get. web_loone08 has made a long list of possible solutions, which you just ignored.

    You cannot access other domains with JavaScript. It's not allowed. If it was, that would be a horrible security hole, because other websites could use JavaScript to telecontrol your browser and, for example, make PayPal transactions on your behalf. So what you're trying there is impossible.

    If you want to count words on another domain, you need a server script (written in PHP or whatever). But web_loone08 already said this, so simply read his post.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    10
    Rep Power
    0
    because what I mean is get text from other html file and count the occurence of the word using java script ??
  14. #8
  15. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    I understand what you're trying to do, but you cannot do this with JavaScript. You just can't. It's impossible. It's not allowed for security reasons.

    You need to count the words on your server with PHP or Ruby or Python or Perl or whatever server-side scripting language you are familiar with.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo