0

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?

6
  • what do you get on console? Commented Jun 4, 2016 at 7:08
  • you can directly get that all options, why you use that library, is there any reason? Commented Jun 4, 2016 at 7:14
  • Obviously, at time you initialoze the plugin, element select isnt available in DOM. Try using dynamic scraper instead Commented Jun 4, 2016 at 7:14
  • @A.Wolff is right you have to place these code in document ready or need to make some delay on script so the HTML will load completely. Commented Jun 4, 2016 at 7:16
  • @A.Wolff I changed to dynamic and added some timeout, but this time nothing is logged on console. Commented Jun 4, 2016 at 7:22

1 Answer 1

1

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);
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.