Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js and Cheerio parsing table with selectors

I'm trying to parse an HTML table using Node.js and Cheerio and am getting some results but unfortunately I'm getting way too much data and am not sure how to parse it down further to get only the data I need.

Here is the small bit of code I've got going so far..

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

request('http://www.myURL.com', function(error, response, body) {

  var $ = cheerio.load(body);

  $('td').each(function() {
    console.log($(this).text());

  });
});

Using a Chrome plugin to locate the selector, I've found that I need ".clickableRow td" but every way I've tried to plug this in doesn't seem to work.

For a little more clarity, the html source looks like this -

<html>
 <body>
  <form>
   <table>
    <tbody>
     <td>
      <table class="standardTable">
       <tbody>
        <tr class="clickableRow">
         <td>first thing I want</td>
         <td>second thing I want</td>
         <td>third thing I want</td>
         <td>fourth thing I want</td>

Does that make sense? The items I want are pretty deep into the HTML and I'm not sure how to get down to that level. Any help would be greatly appreciated! Thanks!

like image 443
Christian Avatar asked Oct 19 '13 23:10

Christian


People also ask

How do you get attributes in Cheerio?

Attributes can be retrieved with attr function. import fetch from 'node-fetch'; import { load } from 'cheerio'; const url = 'http://webcode.me'; const response = await fetch(url); const body = await response. text(); let $ = load(body); let lnEl = $('link'); let attrs = lnEl. attr(); console.


Video Answer


1 Answers

Just use the selector '.clickableRow td'.

like image 65
Peter Lyons Avatar answered Oct 24 '22 05:10

Peter Lyons