Frederik 84
Frederik 84

Reputation: 61

XMLHttpRequest to scrape data from websites

Hi I was hoping someone could help me get this code working I'm trying to make a web scraper script that I can launch from an button. Been searching through the forums for a couple of hours and so far I have come up with this:

function GetData()  {
  Var URL = "http://www.livescore.cz/"
  var XMLReq = new XMLHttpRequest();

  XMLReq.open( "GET", "https://edition.cnn.com/", false )
  XMLReq.send();

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

But I guess I most have done something wrong. Any help would be much appreciated

frederik

Upvotes: 1

Views: 2827

Answers (1)

Peter
Peter

Reputation: 1822

You need to add event listener before the event fired.

function GetData()  {
  var XMLReq = new XMLHttpRequest();

  XMLReq.open( "GET", "https://edition.cnn.com/", false )


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

  XMLReq.send();
}

document.querySelector("#getDataBtn").addEventListener('click', GetData);
<button id="getDataBtn">Get Data</button>

Upvotes: 1

Related Questions