Document querySelector on a variable?


<!DOCTYPE html>
<html>
<head> 
<title>JS Scrape Example</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">

$.get('https://cors-anywhere.herokuapp.com/http://www.whoishostingthis.com/?q=mybb.com', function(response) {    
var selector = '#site > div > div > div.pure-u-1.pure-u-lg-1-3.cloudflare > div > div:nth-child(3) > ul > li:nth-child(1) > a';
var info = $(selector).first();
console.log(info);
});

</script>
</body>
</html>

I’m working on the above code. The goal being to scrape the webhost (in this case Cloudflare). I have the selector found through Chrome’s developer console. However I cannot use javascript’s built in querySelector on the value of a variable (Even though it’s HMTL). What is the best way to go about continuing to scrape this information?

Not sure if there is any easy way to do this in javascript, but it is super easy in Node:

var request = require('request');
var cheerio = require('cheerio');

module.exports = function(done){
  var url = "https://www.packtpub.com/packt/offers/free-learning"; 

  request(url, function(err, res, html){
    if(!err){
      var $ = cheerio.load(html);

      var title = $(".dotd-title h2").html().trim();

      return done(title);
    }
  });
}

This gets the title of the free ebook on packtpub.com

1 Like