Jan Hennemann Jan Hennemann - 1 year ago 286
Javascript Question

Cheerio direct child selector

Hey guys and ladies first of all this is my first question here in stackoverflow so don't be so hard on me.. but w/e :P.
I have a problem..
i'm totally new to web scraping and at the moment i have the problem that i can't select the right elements. My code looks like this:

var express = require('express');
var path = require('path');
var request = require('request');
var cheerio = require('cheerio');
var fs = require('fs');

var app = express();
var port = 8000;

var url = "http://www.finanzparasiten.de/html/links/awd.html";

request(url, function (err, resp, body) {
if(!err) {
var $ = cheerio.load(body)

var test = $('body table table table > tbody > tr > td > p');
test.each(function (ii, asdf) {
var rr = $(asdf).find("table").find("tr").first().find('td:nth-child(2)').text();
} else {
console.log("we encountered an error: " + err);

console.log('server is listening on ' + port);

It keeps logging NULL for the variable test.
It seems like cheerio has problems with the > selector. With jQuery this selection would work as expected.

Thanks for your times and effort.

Answer Source

as far as I remember (when I used cheerio the last time) tbody is not recognized in cheerio, just leave it and use this instead:

table > tr > td

PS: thead was working

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download