erdomester
erdomester

Reputation: 11829

Read div value from webpage

I haven't really found anything about this. I want to read data from a website. From my webpage I can read a div's value with

<div class="tools">hammer</div>

var divs = document.getElementsByTagName("div");
    for(var i=0; i<divs.length; i++ ) {
        if(divs[i].className == "tools") {
            alert(divs[i].innerHTML);
        }
    }

Is it possible to set the URL of the desired webpage so this code would scrape that page? I know the classname and the url.

Based on @cereallarceny's answer I created this code block but I see no alert when running the script:

$.ajax({
type: 'get',
url: 'https://play.google.com/store/apps/details?id=com.viber.voip',
crossDomain: true, //Very important, ensures you can get data from a domain that isn't your own!
success: function() {
  var divs = document.getElementsByTagName("div");
    for(var i=0; i<divs.length; i++ ) {
        if(divs[i].className == "votes") {
            alert(divs[i].innerHTML);
        }
    }
}
});

Upvotes: 1

Views: 1749

Answers (2)

cereallarceny
cereallarceny

Reputation: 4983

If you're using a library like jQuery then you could simply use the load() function which gets the code of a webpage (or a partial of a webpage if you append a #myDiv to the URL). You could then handle that information if you put it in a variable. If you're just using Javascript then you'd need to look into making an AJAX request (which is what load() does). You can find more information on how to do this located here.

For more information on the load() function, read jQuery's documentation.

Keep in mind, jQuery's load() function is for loading HTML into an element. If you want to read and manipulate that data then you should probably use the ajax() function. This way you could do something like the following:

$.ajax({
    type: 'get',
    url: 'http://www.google.com',
    crossDomain: true, //Very important, ensures you can get data from a domain that isn't your own!
    success: function(data) {
        $('#myDiv').html(data);

        //Now I can handle all the HTML from my URL from a <div> tag called #myDiv, the following will alert out the body of http://www.google.com
        alert($('#myDiv').find('body'));
    }
});

This will essentially make a GET request to the specified URL, while noting that it is not of the same domain name as the request's origin (your server) and then handles the success in a function with the HTML returned in a variable data. You can use data however you please now seeing as it's your variable now, including parsing of that information how you please.

Upvotes: 4

Mitch Bukaner
Mitch Bukaner

Reputation: 181

$file=fopen($URL,'r'); 
    if ($file) 
    { 
    $string =""; 
    while (!feof($file)){ 
            $string .=fgets($file,512); 
    } 
echo $string; 
}else echo "error";

this echoes the web. but you can do with it what ever.

Ok. here is a how to ask for a webSite in js

var xmlhttp;

if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }


   xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    return xmlhttp.responseText;
    }
  }


var answerFromURL=xmlhttp.onreadystatechange();


    xmlhttp.open("GET","urlToParse.php",true);
    xmlhttp.send();

Upvotes: 0

Related Questions