使用nodejs cheerio深层嵌套元素标签抓取网站

时间:2019-07-16 08:49:23

标签: node.js web-scraping cheerio

我正在尝试从网站上抓取文字,但似乎无法提取任何内容。

下面是结构和代码。

enter image description here

我的代码:

const rp = require("request-promise");
const $ = require("cheerio");
const url = "xx";

rp(url)
  .then(function(html) {
    //success!
    let token = "ce-bodytext";
    console.log($(token, response).length);
    console.log($(token, html)).text;
  })
  .catch(function(err) {
    console.log(JSON.stringify(err));
  });

虽然我只需要文本,但是标签没有id。 另外,我希望ce-bodytext将按顺序

提取所有值,但我得到的只是空输出。

{}

如何仅提取图像中显示的文本?

2 个答案:

答案 0 :(得分:2)

尝试一下:

let token = ".ce-bodytext>p>strong>font>font";
console.log($(token, html).text());

答案 1 :(得分:1)

ce-bodytextclass,您忘记在其前面添加.

const token = '.ce-bodytext';

它将至少修复空输出。

相关问题