gpt4 book ai didi

node.js - Cheerio 给出奇怪的结果

转载 作者:太空宇宙 更新时间:2023-11-04 01:31:56 26 4
gpt4 key购买 nike

这是一个简单的cheerio应用程序。该标签有一个名为product-link的类,我想访问它的href,但是当我控制台记录它时,我没有得到任何html。任何人都可以了解正在发生的事情以及如何从中获取我想要的数据吗?

let holder = $('.product-link');
console.log(holder);

结果->

initialize {
options:
{ withDomLvl1: true,
normalizeWhitespace: false,
xml: false,
decodeEntities: true },
_root:
initialize {
'0':
{ type: 'root',
name: 'root',
namespace: 'http://www.w3.org/1999/xhtml',
attribs: [Object: null prototype] {},
'x-attribsNamespace': [Object: null prototype] {},
'x-attribsPrefix': [Object: null prototype] {},
children: [Array],
parent: null,
prev: null,
next: null },
options:
{ withDomLvl1: true,
normalizeWhitespace: false,
xml: false,
decodeEntities: true },
length: 1,
_root: [Circular] },
length: 0,
prevObject:
initialize {
'0':
{ type: 'root',
name: 'root',
namespace: 'http://www.w3.org/1999/xhtml',
attribs: [Object: null prototype] {},
'x-attribsNamespace': [Object: null prototype] {},
'x-attribsPrefix': [Object: null prototype] {},
children: [Array],
parent: null,
prev: null,
next: null },
options:
{ withDomLvl1: true,
normalizeWhitespace: false,
xml: false,
decodeEntities: true },
length: 1,
_root: [Circular] } }

最佳答案

cheerio 有一个类似于 jQuery 的 API。所以当你调用这个函数 $('selector') Cheerio 会返回一个带有很多方法和字段的对象来获取属性或查找子 Node 等。这就是为什么它不返回 html。但是,如果您想查看带下划线的 HTML,则可以使用 $('selector').html()

如果您想访问属性,可以使用 attr() 方法。

所以现在你的代码将是

const holder = $('.product-link');
const href = holder.attr('href');

您可以引用文档

https://github.com/cheeriojs/cheerio

https://api.jquery.com/ (因为 Cheerio 的设计类似于 jQuery)

关于node.js - Cheerio 给出奇怪的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55835928/

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