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.

SyntaxError: Unmatched selector: tbody tr

See original GitHub issue

I’m using cheerio js 1.0.0-rc.3 to scrape a website. I also tried with 1.0.0-rc.2. Both have the same behavior

console.log($('table tbody tr')) Above console is printing all objects. However, when I tried to loop through the elements, like given below

$('table tbody tr').each(function (index, element) {
}

I’m getting the following error.

SyntaxError: Unmatched selector: tbody tr

Any insight on this error will be great help

Edit

Given below is the table structure I’m trying to scrape.

<table border="1" cellspacing="1" cellpadding="1" class="table table-striped" style="font-weight:bold">
<thead>
    <tr>
    <th><strong>Heading</strong></th>
    <th><strong>Heading</strong></th>
    <th><strong>Heading</strong></th>                              
    <th><strong>Heading</strong></th>
    <th><strong>Heading</strong></th>
    </tr>
</thead>    
<tbody>
    <tr>
    <td>row 1 col 1</td>
    <td>row 1 col 2</td>
    <td>row 1 col 3</td>
    <td>row 1 col 4</td>
    <td>row 1 col 5</td>
    </tr>

    <tr>
    <td>row 2 col 1</td>
    <td>row 2 col 2</td>
    <td>row 2 col 3</td>
    <td>row 2 col 4</td>
    <td>row 2 col 5</td>
    </tr>

</tbody>
</table>

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
5saviahvcommented, Jun 9, 2020

I am puzzled, it still working.

My entire test code:

const cheerio = require('cheerio');

var html = `<table border="1" cellspacing="1" cellpadding="1" class="table table-striped" style="font-weight:bold">
<thead>
    <tr>
    <th><strong>Heading</strong></th>
    <th><strong>Heading</strong></th>
    <th><strong>Heading</strong></th>
    <th><strong>Heading</strong></th>
    <th><strong>Heading</strong></th>
    </tr>
</thead>
<tbody>
    <tr>
    <td>row 1 col 1</td>
    <td>row 1 col 2</td>
    <td>row 1 col 3</td>
    <td>row 1 col 4</td>
    <td>row 1 col 5</td>
    </tr>

    <tr>
    <td>row 2 col 1</td>
    <td>row 2 col 2</td>
    <td>row 2 col 3</td>
    <td>row 2 col 4</td>
    <td>row 2 col 5</td>
    </tr>

</tbody>
</table>`;


var $ = cheerio.load(html);
/* 
$('table tbody tr').each(function(i,a){
    console.log('Index: %s',i);
    console.log(a);
    console.log(cheerio.text([a]));
})
//*/


const ths = $('th').toArray(); // gets th's in thead
console.log(`ths length:\t${ths.length}`);

const trs = $('tr').toArray(); // gets tr's in thead, but not tr's in tbody
console.log(`trs length:\t${trs.length}`);

const tds = $('td').toArray(); // does not work 
console.log(`tds length:\t${tds.length}`);

and my results are:

ths length:	5
trs length:	3
tds length:	10

so all items are there

and I used cheerio version “1.0.0-rc.3” from npm.

0reactions
joshxyzhimselfcommented, Jun 9, 2020
Read more comments on GitHub >

github_iconTop Results From Across the Web

SyntaxError: Unmatched selector: '.next' · Issue #710 - GitHub
Selector : div#pagination a.results:has('.next') [ERROR] 11:32:40 SyntaxError SyntaxError: Unmatched selector: '.next' at parse ...
Read more >
Cheerio unmatched selector error while selecting plain text
map see the 'TEXT WITHOUT TAG', it throws an error like this: Unmatched selector: ... Which is expected because it hasn't any selectors....
Read more >
jquery.js
1793 markFunction(function( seed, matches ) { 1794 var idx, 1795 matched = fn( seed, ... 1859 // http://www.w3.org/TR/selectors/#lang-pseudo 1860 "lang": ...
Read more >
Team:Imperial College/Resources/JS:JQuery - iGEM 2016
Build a new jQuery matched element set var ret = jQuery.merge( ... Attribute selectors: http://www.w3.org/TR/selectors/#attribute-selectors attributes ...
Read more >
Converse.js API Documentation Source: headless/dist ...
Let n be the number given in // the first cell of the same row. ... Sizzle.tokenize = function( selector, parseOnly ) {...
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