question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pass cheerio selectors as a variable

See original GitHub issue

I couldn’t get any responses on other forums so I’ll try here although this is technically not a bug report. Is it possible to do something like this with cheerio?

This is a simplified example. I need to somehow store cheerio selectors in a variable or object and then call request function repeatedly with different set of selectors.

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


// None of the below approaches work
// var person = "$('li.person')";  <-- this one is passed as a string
// var person = $('li.person');  <-- this one gives error because $ is not defined

request( 'http://example.com', function(error, response, body) {
  $ = cheerio.load(body);
  var $people = person;

  $people.each(function() {
    console.log('Person: ' + $(this).text());
  }
});

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

6reactions
jugglinmikecommented, Oct 30, 2013

@finspin You might be able to do something quick-and-dirty with eval, but I wouldn’t recommend it. Instead, I can think of two options:

1. Define a more expressive data structure that you can “map” to selections when you’re ready to scrape the page, i.e.

var sites = {
  site1: {
    name: "Site 1",
    url: "http://example1.com",
      selector: ["li.person", "a"]
  }
};

…this is nice because it’s declarative (so you can easily save it to a database), but it will also take some extra thought: you’ll have to design an expressive declarative form for traversing documents, and you’ll have to intelligently iterate through it when the time comes to scrape.

2. Define a function that accepts the “loaded” Cheerio object and makes the selection as necessary:

var sites = {
  site1: {
    name: "Site 1",
    url: "http://example1.com",
    scrape: function($) {
      $('li.person').each(function() {
        console.log($(this).find('a').text());
      });
    }
  }
};

// later...
      function(error, response, body) {
        var $ = cheerio.load(body);
        company.scrape($);
      }

0reactions
jugglinmikecommented, Oct 31, 2013

I’m glad it’s working out for you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can I add a variable to a cheerio selector? - Stack Overflow
1 Answer 1 ... I managed to do it using: var fullTable = $('tr[id="'+variable_goes_here+'"]');. Thanks!
Read more >
Class Cheerio<T> Abstract
Iterates over a cheerio object, reducing the set of selector elements to those that match the selector or pass the function's test. This...
Read more >
Cheerio tutorial - web scraping in JavaScript with ... - ZetCode
First, we load the HTML document. To mimic jQuery, we use the $ variable. let title = $('title');. The selector returns the title ......
Read more >
Javascript – Cheerio: How to select element by text content – iTecNote
cheerio javascriptjquery-selectors ... And remember: functions in JavaScript can be passed around like variables (first-class functions), meaning these ...
Read more >
Using the Cheerio NPM Package for Web Scraping
As you can see, all you need to do is pass an HTML string into Cheerio's load method. Cheerio itself doesn't provide any...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found