Cheerio - Get correct text when selector returns more than one result

alexpfx

https://www.cbf.com.br/futebol-brasileiro/competicoes/campeonato-brasileiro-serie-a/2018/1?ref=botao

enter image description here

I would like to get the market text from the page above. ("Sábado, 14 de Abril de 2018" and "16:00").

I did this with kotlin and the jsoup library:

val date = select("div.col-sm-8 > span.text-2")[1] //Sábado, 14 de Abril de 2018
val time = select("div.col-sm-8 > span.text-2")[2] //16:00

This query div.col-sm-8 > span.text-2 returns an array and I simple get the right information using the index.

But due to other issues, I have to use javascript.

I tried to do the same thing using JavaScript and the Cherio library but it seems that it doesn't work the same way, even if both search mode are based in JQuery:

const scherio = require('cheerio');
const rp = require('request-promise');

/**
 * @type {string}
 */
const baseurl = "https://www.cbf.com.br/futebol-brasileiro/competicoes/campeonato-brasileiro-serie-a/2018/";

const turn = 190;

let totalGames = 1;
const gamesPerRound = 10;

module.exports =

    class FetchRoundsFromCbf {

        fetchRounds() {

            for (let i = 1; i <= totalGames; i++) {
                let url = baseurl.concat(i.toString());
                rp(url).then(function (html) {
                    const $ = scherio.load(html);


                    let date = $("div.col-sm-8 > span.text-2")[1];
                    let time = $("div.col-sm-8 > span.text-2")[2];


                     console.log(date.text());
                     console.log(time.text());
                });

            }
        }

    }

Gives me:

Unhandled rejection TypeError: date.text is not a function
    at /home/alexandre/dev/flutter/brasileiro-parser-js/network/fetchdata/FetchRoundsFromCbf.js:32:39
    at tryCatcher (/home/alexandre/dev/flutter/brasileiro-parser-js/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/home/alexandre/dev/flutter/brasileiro-parser-js/node_modules/bluebird/js/release/promise.js:512:31)
    at Promise._settlePromise (/home/alexandre/dev/flutter/brasileiro-parser-js/node_modules/bluebird/js/release/promise.js:569:18)
    at Promise._settlePromise0 (/home/alexandre/dev/flutter/brasileiro-parser-js/node_modules/bluebird/js/release/promise.js:614:10)
    at Promise._settlePromises (/home/alexandre/dev/flutter/brasileiro-parser-js/node_modules/bluebird/js/release/promise.js:694:18)
    at _drainQueueStep (/home/alexandre/dev/flutter/brasileiro-parser-js/node_modules/bluebird/js/release/async.js:138:12)
    at _drainQueue (/home/alexandre/dev/flutter/brasileiro-parser-js/node_modules/bluebird/js/release/async.js:131:9)
    at Async._drainQueues (/home/alexandre/dev/flutter/brasileiro-parser-js/node_modules/bluebird/js/release/async.js:147:5)
    at Immediate.Async.drainQueues [as _onImmediate] (/home/alexandre/dev/flutter/brasileiro-parser-js/node_modules/bluebird/js/release/async.js:17:14)
    at processImmediate (timers.js:637:19)

Then I print only the query result:

 console.log(date);
 console.log(time);

I receive:

{ type: 'tag',
  name: 'span',
  namespace: 'http://www.w3.org/1999/xhtml',
  attribs: [Object: null prototype] { class: 'text-2 p-r-20' },
  'x-attribsNamespace': [Object: null prototype] { class: undefined },
  'x-attribsPrefix': [Object: null prototype] { class: undefined },
  children:
   [ { type: 'tag',
       name: 'i',
       namespace: 'http://www.w3.org/1999/xhtml',
       attribs: [Object],
       'x-attribsNamespace': [Object],
       'x-attribsPrefix': [Object],
       children: [],
       parent: [Circular],
       prev: null,
       next: [Object] },
     { type: 'text',
       data: ' Sábado, 14 de Abril de 2018',
       parent: [Circular],
       prev: [Object],
       next: null } ],
  parent:
   { type: 'tag',
     name: 'div',
     namespace: 'http://www.w3.org/1999/xhtml',
     attribs: [Object: null prototype] { class: 'col-sm-8' },
     'x-attribsNamespace': [Object: null prototype] { class: undefined },
     'x-attribsPrefix': [Object: null prototype] { class: undefined },
     children:
      [ [Object],
        [Object],
        [Object],
        [Circular],
        [Object],
        [Object],
        [Object] ],
     parent:
      { type: 'tag',
        name: 'div',
        namespace: 'http://www.w3.org/1999/xhtml',
        attribs: [Object],
        'x-attribsNamespace': [Object],
        'x-attribsPrefix': [Object],
        children: [Array],
        parent: [Object],
        prev: [Object],
        next: [Object] },
     prev:
      { type: 'text',
        data: '\n            ',
        parent: [Object],
        prev: null,
        next: [Circular] },
     next:
      { type: 'text',
        data: '\n            ',
        parent: [Object],
        prev: [Circular],
        next: [Object] } },
  prev:
   { type: 'text',
     data: '\n                              ',
     parent:
      { type: 'tag',
        name: 'div',
        namespace: 'http://www.w3.org/1999/xhtml',
        attribs: [Object],
        'x-attribsNamespace': [Object],
        'x-attribsPrefix': [Object],
        children: [Array],
        parent: [Object],
        prev: [Object],
        next: [Object] },
     prev:
      { type: 'tag',
        name: 'span',
        namespace: 'http://www.w3.org/1999/xhtml',
        attribs: [Object],
        'x-attribsNamespace': [Object],
        'x-attribsPrefix': [Object],
        children: [Array],
        parent: [Object],
        prev: [Object],
        next: [Circular] },
     next: [Circular] },
  next:
   { type: 'text',
     data: '\n                ',
     parent:
      { type: 'tag',
        name: 'div',
        namespace: 'http://www.w3.org/1999/xhtml',
        attribs: [Object],
        'x-attribsNamespace': [Object],
        'x-attribsPrefix': [Object],
        children: [Array],
        parent: [Object],
        prev: [Object],
        next: [Object] },
     prev: [Circular],
     next:
      { type: 'tag',
        name: 'span',
        namespace: 'http://www.w3.org/1999/xhtml',
        attribs: [Object],
        'x-attribsNamespace': [Object],
        'x-attribsPrefix': [Object],
        children: [Array],
        parent: [Object],
        prev: [Circular],
        next: [Object] } } }
{ type: 'tag',
  name: 'span',
  namespace: 'http://www.w3.org/1999/xhtml',
  attribs: [Object: null prototype] { class: 'text-2 p-r-20' },
  'x-attribsNamespace': [Object: null prototype] { class: undefined },
  'x-attribsPrefix': [Object: null prototype] { class: undefined },
  children:
   [ { type: 'tag',
       name: 'i',
       namespace: 'http://www.w3.org/1999/xhtml',
       attribs: [Object],
       'x-attribsNamespace': [Object],
       'x-attribsPrefix': [Object],
       children: [],
       parent: [Circular],
       prev: null,
       next: [Object] },
     { type: 'text',
       data: ' 16:00',
       parent: [Circular],
       prev: [Object],
       next: null } ],
  parent:
   { type: 'tag',
     name: 'div',
     namespace: 'http://www.w3.org/1999/xhtml',
     attribs: [Object: null prototype] { class: 'col-sm-8' },
     'x-attribsNamespace': [Object: null prototype] { class: undefined },
     'x-attribsPrefix': [Object: null prototype] { class: undefined },
     children:
      [ [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Circular],
        [Object] ],
     parent:
      { type: 'tag',
        name: 'div',
        namespace: 'http://www.w3.org/1999/xhtml',
        attribs: [Object],
        'x-attribsNamespace': [Object],
        'x-attribsPrefix': [Object],
        children: [Array],
        parent: [Object],
        prev: [Object],
        next: [Object] },
     prev:
      { type: 'text',
        data: '\n            ',
        parent: [Object],
        prev: null,
        next: [Circular] },
     next:
      { type: 'text',
        data: '\n            ',
        parent: [Object],
        prev: [Circular],
        next: [Object] } },
  prev:
   { type: 'text',
     data: '\n                ',
     parent:
      { type: 'tag',
        name: 'div',
        namespace: 'http://www.w3.org/1999/xhtml',
        attribs: [Object],
        'x-attribsNamespace': [Object],
        'x-attribsPrefix': [Object],
        children: [Array],
        parent: [Object],
        prev: [Object],
        next: [Object] },
     prev:
      { type: 'tag',
        name: 'span',
        namespace: 'http://www.w3.org/1999/xhtml',
        attribs: [Object],
        'x-attribsNamespace': [Object],
        'x-attribsPrefix': [Object],
        children: [Array],
        parent: [Object],
        prev: [Object],
        next: [Circular] },
     next: [Circular] },
  next:
   { type: 'text',
     data: '\n                          ',
     parent:
      { type: 'tag',
        name: 'div',
        namespace: 'http://www.w3.org/1999/xhtml',
        attribs: [Object],
        'x-attribsNamespace': [Object],
        'x-attribsPrefix': [Object],
        children: [Array],
        parent: [Object],
        prev: [Object],
        next: [Object] },
     prev: [Circular],
     next: null } }

I'm not very good at javascript, how would I do to retrieve the information I need?

Guillermo Gutiérrez

You can use eq() to get a Cheerio element by index, the same way as in jQuery.

let date = $("div.col-sm-8 > span.text-2").eq(1);
let time = $("div.col-sm-8 > span.text-2").eq(2);

eq() reduces the set of matched elements to the one at the specified index.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

JPA Repository: javax.persistence.NonUniqueResultException: result returns more than one elements

How to write more than one object result to one output text file in Java?

How to remove more than one space when reading text file

Protractor more than one element found for locator By(css selector, .image-holder) - the first result will be used

Cannot get more than one result when using System.Linq.Dynamic.Core to query a list of objects that have a member dictionary

Mongoose returning more than one result

Is it okay to have more than one ":root" selector?

Mongoose query returning more than one result

Taking an average of list columns when more than one result exisist

Mysql Subquery returns only one result, even with more than one ID in the list

Does Hashtable get method returns more than one value?

Get_terms returns error when more than one custom taxonomy exists

How to get more than one value of text box using in condition

.removeClass for more than one cached selector

MySQL subquery returns more than one row when using not equal

Why am I getting result consisted of more than one row error, when finally one row is returned?

nlapiSearchRecord returning more than one result

More than one result in a field

Mysql Join two tables into one output but result Subquery returns more than 1 row

Ressource returns null when more than one language is in the project

How to get more than one result in XQuery

Case returns more than one value when using it with join

SimpleXMLElement foreach only shows when there is more than one result

Testcafe: Is there a way to add more than one text as an input for the Selector().withText(text) - Issue when application supporting multiple language

In beautifulsoup4, when scraping a website purely based off of an element and the text within, how do you return more than one result?

Cannot create TypedQuery for query with more than one return using requested result type, despite having correct DTO

multiple input text more than 50 how to get result from one ajax request?

result returns more than one elements

Get different results when loop using more than one value