Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/463.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么.text()和.html()不能与cheerio js和节点获取一起使用?_Javascript_Node.js_Cheerio_Node Fetch - Fatal编程技术网

Javascript 为什么.text()和.html()不能与cheerio js和节点获取一起使用?

Javascript 为什么.text()和.html()不能与cheerio js和节点获取一起使用?,javascript,node.js,cheerio,node-fetch,Javascript,Node.js,Cheerio,Node Fetch,我不熟悉Node JS,使用Node fetch和cheerio包。我试图从不同的网站上获取数据,所以我通过传递许多不同的URL和选择器进行测试。但是,在下面的代码中,无论我将哪个选择器或URL作为输入传递,.text()返回空字符串,.html()返回null const cheerio= require('cheerio'); const fetch = require('node-fetch'); fetch('https://www.npmjs.com/package/node-fet

我不熟悉Node JS,使用Node fetch和cheerio包。我试图从不同的网站上获取数据,所以我通过传递许多不同的URL和选择器进行测试。但是,在下面的代码中,无论我将哪个选择器或URL作为输入传递,.text()返回空字符串,.html()返回null

const cheerio= require('cheerio');
const fetch = require('node-fetch');

fetch('https://www.npmjs.com/package/node-fetch/')
    .then((res)=>{ 
        if(res.ok){       
            let $=cheerio.load(res);
            console.log(res);
            let siteData = $('#readme > p:nth-child(8)');
            console.log(siteData.text());
            console.log(siteData.html());
            return res.text();
        }else{
            throw new Error(res.statusText);
        }
    }) 
    .then(body => console.log(body))       
    .catch(error => console.log(error))
我甚至将res.text()的输出写入了一个文件,并将其与网站的源HTML进行了比较。这几乎是一样的。res的值打印如下:

Response {
  size: 0,
  timeout: 0,
  prev: null,
  next: null,
  root: {
    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: [ [Circular] ],
    parent: null,
    prev: null,
    next: null
  },
  parent: null,
  [Symbol(Body internals)]: {
    body: Gunzip {
      _writeState: [Uint32Array],
      _readableState: [ReadableState],
      readable: true,
      _events: [Object: null prototype],
      _eventsCount: 6,
      _maxListeners: undefined,
      _writableState: [WritableState],
      writable: true,
      allowHalfOpen: true,
      _transformState: [Object],
      _hadError: false,
      bytesWritten: 0,
      _handle: [Zlib],
      _outBuffer: <Buffer 80 00 f4 9f 03 02 00 00 f0 80 f2 9f 03 02 00 00 20 46 00 00 00 00 00 00 d8 73 dd 9f 03 02 00 00 0f 00 00 00 7f ae f8 39 01 5d dd 9f 03 02 00 00 d0 68 ... 16334 more bytes>,
      _outOffset: 0,
      _chunkSize: 16384,
      _defaultFlushFlag: 2,
      _finishFlushFlag: 2,
      _defaultFullFlushFlag: 3,
      _info: undefined,
      _level: -1,
      _strategy: 0,
      [Symbol(kCapture)]: false
    },
    disturbed: false,
    error: null
  },
  [Symbol(Response internals)]: {
    url: 'https://www.npmjs.com/package/node-fetch',
    status: 200,
    statusText: 'OK',
    headers: Headers { [Symbol(map)]: [Object: null prototype] },
    counter: 1
  }
}
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]
  }
}
不过,siteData.text()是一个空字符串,siteData.html()是null。请告诉我这段代码有什么问题,我已经浏览了很多堆栈溢出页面,并阅读了cheerio文档,但我仍然没有找到答案

提前谢谢。

您想等待res.text()我相信并加载它。您想等待res.text()我相信并加载它。