1

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);
        }
    }
}
});
erdomester
  • 11,789
  • 32
  • 132
  • 234

2 Answers2

4

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.

Community
  • 1
  • 1
cereallarceny
  • 4,913
  • 4
  • 39
  • 74
  • No @jgillich... that's the load **event** in jQuery. I'm talking about the load function. http://api.jquery.com/load/ – cereallarceny Apr 13 '13 at 17:52
  • This looks good. Should I put my code into the function? I mean ` success: function(data) { my whole js code }` – erdomester Apr 13 '13 at 18:11
  • mmm this answer is probably better than mine – Mitch Bukaner Apr 13 '13 at 18:12
  • You could put the code in there but I doubt it'd work without some jimmyrigging. :) My code uses jQuery (http://jquery.com), if you're going to use any jQuery then you might as well have your code be jQuery as well, which it isn't. This way you stick to conformity within your code. – cereallarceny Apr 13 '13 at 18:13
  • I just added that code block to my question. I have jquery else in the code so the necessary jquery version is imported in the file. What else do I need? – erdomester Apr 13 '13 at 18:17
  • Check my edited response... it should better suit your needs. This way you can work with the HTML using jQuery and not via parsing a string on your own. You need to include the jQuery library in your source code if you haven't. Read this article on how to set up jQuery: http://www.jquery4u.com/tutorials/setup-jquery/ – cereallarceny Apr 13 '13 at 18:21
  • Was I able to help you @erdomester? – cereallarceny Apr 13 '13 at 20:00
  • You were of great help but it turned out I didn't javascript as I managed to do it from Android (I thought I need to run a js script) by reading the whole page and looking for the div value. The load() function looked Chinese to me. If you could provide me with a full code I would be grateful, because I may need this later. – erdomester Apr 15 '13 at 20:26
  • Sounds good to me man! So long as you got the right solution, that's all that matters, right? Thanks for the answer accept! – cereallarceny Apr 15 '13 at 20:28
  • I'm sorry I would never ask for this, but like I said I couldn't use load(). The ajax code you provided didn't return anything – erdomester Apr 15 '13 at 20:34
  • Load would work like such: `$("#myDiv").load("http://www.google.com");`. However, I'm not sure this would work because of Javascript requiring loads to be done on the same domain... hence why it wouldn't return anything. Check your developer console for any Javascript errors, that should do some explaining as to why you didn't get a return. – cereallarceny Apr 15 '13 at 20:38
0
$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();
Mitch Bukaner
  • 171
  • 13