Pass cheerio selectors as a variable
See original GitHub issueI 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:
- Created 10 years ago
- Comments:7 (7 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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.
…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:
I’m glad it’s working out for you!