brainmassage
brainmassage

Reputation: 1254

How to use jquery selector on nested html

This is my html:

<select id="my-select">
  <option value="1100">First option</option>
  <option value="1200">First option</option>
  <option value="1300">First option</option>
</select>

I am trying out scraperjs library to parse HTML. In their github page, there is this example:

var scraperjs = require('scraperjs');
scraperjs.StaticScraper.create('https://news.ycombinator.com/')
  .scrape(function($) {
    return $(".title a").map(function() {
        return $(this).text();
    }).get();
  })
  .then(function(news) {
    console.log(news);
  })

Then I try the same thing on my HTML but it does not work. I only change this:

var scraperjs = require('scraperjs');
scraperjs.StaticScraper.create('https://news.ycombinator.com/')
  .scrape(function($) {
    return $("#my-select option").map(function() {
      return $(this).attr("value");
    }).get();
  })
  .then(function(news) {
    console.log(news);
  })

What is wrong with my jquery selector?

Upvotes: 0

Views: 72

Answers (1)

Paulo Ramos
Paulo Ramos

Reputation: 195

You're still using the url(https://news.ycombinator.com/) from the example, replace the url to the html page you want to scrape:

var scraperjs = require('scraperjs');
var yourUrl = 'http://www.yoursite.com/yourpage';
scraperjs.StaticScraper.create(yourUrl)
 .scrape(function($) {
 return $("#my-select option").map(function() {
  return $(this).attr("value");
}).get();
})
.then(function(options) {
 console.log(options);
})

Upvotes: 1

Related Questions