gpt4 book ai didi

javascript - Cheerio - 当选择器返回多个结果时获取正确的文本

转载 作者:行者123 更新时间:2023-11-30 19:44:32 26 4
gpt4 key购买 nike

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

enter image description here

我想从上面的页面获取市场文本。 (“Sábado,2018 年 4 月 14 日”和“16:00”)。

我用 kotlin 和 jsoup 库做了这个:

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

此查询 div.col-sm-8 > span.text-2 返回一个数组,我使用索引简单地获取了正确的信息。

但由于其他问题,我不得不使用javascript。

我尝试使用 JavaScript 和 Cherio 库来做同样的事情,但它的工作方式似乎并不相同,即使这两种搜索模式都基于 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());
});

}
}

}

给我:

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)

然后我只打印查询结果:

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

我收到:

{ 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 } }

我不太擅长 javascript,我该怎么做才能检索到我需要的信息?

最佳答案

您可以使用 eq()通过索引获取 Cheerio 元素,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() 将匹配元素集缩减为指定索引处的元素。

关于javascript - Cheerio - 当选择器返回多个结果时获取正确的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55069621/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com