Search code examples
web-scrapingphantomjscasperjs

Casper JS Ajax request is not returning any response


I am working on casper JS, to scrap data from a website. For now I am just getting the title of a website. When I scrap the title of that page I want to send that title to my php script through Casper JS ajax method, but for some reasons its not working for me :

Below is the Casper JS code :

var casper = require('casper').create();

casper.start("https://www.google.com/", function() {});

casper.then(function() {
  var d = this.evaluate(function() {
    var links = document.getElementsByTagName('title')[0].textContent;
    return links;
  })
  console.log(d);
  casper.thenOpen("modal_scripts.php?scraped=true", {
      method: "post",
      data: {
        data: d
      }
    },
    function(response) {
      console.log(response.data);
    });
})


casper.run();

And this is the php scripts, where I want to collect the data sent by casper POST method.

if (isset($_POST["scraped"])) {

  $d = $_POST["data"];

  echo "Response : "." ".$d;

}

I just want to send the scrapped data to my php script, where I can save it in a Database.


Solution

  • The Simpler Solution :

    var casper = require('casper').create();
    
    casper.start("https://www.google.com/");
    casper.then(function() {
      var data = this.evaluate(function() {
        var title = document.getElementsByTagName('title')[0].textContent;
        return title;
      })
      console.log(data);
      casper.thenOpen("http://localhost/fiverr/Crawl%20The%20Jobs/modal_scripts.php", {
        method: "POST",
        data: data + "&crawled_jobs=true"
      }).then(function(res) {
        console.log(res.status);
      })
    })
    
    casper.run();