2017-08-25 89 views
0

我想从源(链接或文件,...)和 找到它的值。 html格式是:html操作与节点JS

<!doctype html> 
<html> 
<body> 
    <main> 
    <section id="serp"> 
     <div> 
     <article>a</article> 
     <article>b</article> 
     <article>c</article> 
     <article>d</article> 
     </div> 
    </section> 
    </main> 
</body> 
</html> 

首先我用cheerio的。 根据文档我写:

const cheerio = require('cheerio'); 
const $ = cheerio.load(myhtml); 
const content = $('#serp div').children(); 
console.log(content); // null 

根据我使用x-ray相同的程序和jsdom但他们都 打印空。

+0

这是什么'的console.log(MYHTML)'输出,只是在Cheerio加载之前? –

+0

这串<!DOCTYPE HTML>

<节ID = “SERP”>
a
b
c
d
@JeremyThille – Ahmad

+0

如果HTML是实际可用并装入Cheerio,没有理由为什么选择器应该返回null。问题在别处。你是否真的尝试记录它,或者你是否说过,因为你认为这会记录下来? –

回答

0

我已经做了以下内容:

let myhtml = `<!doctype html> 
<html> 
<body> 
    <main> 
    <section id="serp"> 
     <div> 
     <article>a</article> 
     <article>b</article> 
     <article>c</article> 
     <article>d</article> 
     </div> 
    </section> 
    </main> 
</body> 
</html>`; 

const cheerio = require('cheerio'); 
const $ = cheerio.load(myhtml); 
const content = $('#serp div').children(); 
console.log(content); 
console.log(`html: ${content.html()}`); 

它输出以下控制台:

initialize { 
    '0': 
    { type: 'tag', 
    name: 'article', 
    namespace: 'http://www.w3.org/1999/xhtml', 
    attribs: {}, 
    'x-attribsNamespace': {}, 
    'x-attribsPrefix': {}, 
    children: [ [Object] ], 
    parent: 
     { type: 'tag', 
     name: 'div', 
     namespace: 'http://www.w3.org/1999/xhtml', 
     attribs: {}, 
     'x-attribsNamespace': {}, 
     'x-attribsPrefix': {}, 
     children: [Object], 
     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] } }, 
    '1': 
    { type: 'tag', 
    name: 'article', 
    namespace: 'http://www.w3.org/1999/xhtml', 
    attribs: {}, 
    'x-attribsNamespace': {}, 
    'x-attribsPrefix': {}, 
    children: [ [Object] ], 
    parent: 
     { type: 'tag', 
     name: 'div', 
     namespace: 'http://www.w3.org/1999/xhtml', 
     attribs: {}, 
     'x-attribsNamespace': {}, 
     'x-attribsPrefix': {}, 
     children: [Object], 
     parent: [Object], 
     prev: [Object], 
     next: [Object] }, 
    prev: 
     { type: 'text', 
     data: '\n  ', 
     parent: [Object], 
     prev: [Object], 
     next: [Circular] }, 
    next: 
     { type: 'text', 
     data: '\n  ', 
     parent: [Object], 
     prev: [Circular], 
     next: [Object] } }, 
    '2': 
    { type: 'tag', 
    name: 'article', 
    namespace: 'http://www.w3.org/1999/xhtml', 
    attribs: {}, 
    'x-attribsNamespace': {}, 
    'x-attribsPrefix': {}, 
    children: [ [Object] ], 
    parent: 
     { type: 'tag', 
     name: 'div', 
     namespace: 'http://www.w3.org/1999/xhtml', 
     attribs: {}, 
     'x-attribsNamespace': {}, 
     'x-attribsPrefix': {}, 
     children: [Object], 
     parent: [Object], 
     prev: [Object], 
     next: [Object] }, 
    prev: 
     { type: 'text', 
     data: '\n  ', 
     parent: [Object], 
     prev: [Object], 
     next: [Circular] }, 
    next: 
     { type: 'text', 
     data: '\n  ', 
     parent: [Object], 
     prev: [Circular], 
     next: [Object] } }, 
    '3': 
    { type: 'tag', 
    name: 'article', 
    namespace: 'http://www.w3.org/1999/xhtml', 
    attribs: {}, 
    'x-attribsNamespace': {}, 
    'x-attribsPrefix': {}, 
    children: [ [Object] ], 
    parent: 
     { type: 'tag', 
     name: 'div', 
     namespace: 'http://www.w3.org/1999/xhtml', 
     attribs: {}, 
     'x-attribsNamespace': {}, 
     'x-attribsPrefix': {}, 
     children: [Object], 
     parent: [Object], 
     prev: [Object], 
     next: [Object] }, 
    prev: 
     { type: 'text', 
     data: '\n  ', 
     parent: [Object], 
     prev: [Object], 
     next: [Circular] }, 
    next: 
     { type: 'text', 
     data: '\n  ', 
     parent: [Object], 
     prev: [Circular], 
     next: null } }, 
    options: 
    { withDomLvl1: true, 
    normalizeWhitespace: false, 
    xml: false, 
    decodeEntities: true }, 
    _root: 
    initialize { 
    '0': 
     { type: 'root', 
     name: 'root', 
     namespace: 'http://www.w3.org/1999/xhtml', 
     attribs: {}, 
     'x-attribsNamespace': {}, 
     'x-attribsPrefix': {}, 
     children: [Object], 
     parent: null, 
     prev: null, 
     next: null }, 
    options: 
     { withDomLvl1: true, 
     normalizeWhitespace: false, 
     xml: false, 
     decodeEntities: true }, 
    length: 1, 
    _root: [Circular] }, 
    length: 4, 
    prevObject: 
    initialize { 
    '0': 
     { type: 'tag', 
     name: 'div', 
     namespace: 'http://www.w3.org/1999/xhtml', 
     attribs: {}, 
     'x-attribsNamespace': {}, 
     'x-attribsPrefix': {}, 
     children: [Object], 
     parent: [Object], 
     prev: [Object], 
     next: [Object] }, 
    options: 
     { withDomLvl1: true, 
     normalizeWhitespace: false, 
     xml: false, 
     decodeEntities: true }, 
    _root: initialize { '0': [Object], options: [Object], length: 1, _root: [Circular] }, 
    length: 1, 
    prevObject: initialize { '0': [Object], options: [Object], length: 1, _root: [Circular] } } } 
html: a 

Process finished with exit code 0