用 cheerio 清空输出

Empty output scraping with cheerio

最近几天我一直在玩 Node 和 CheerioJS,玩得很开心。我想抓取此网页,但由于某种原因,我得到了一个空字符串。很奇怪,因为这种确切的方法似乎适用于其他网站。

这是我设置的:

const rp = require('request-promise');
const cheerio = require('cheerio');
const bread = {
  uri: `https://www.ah.nl/producten/product/wi112339/ah-tijger-bruin-heel`,
  transform: function(body) {
    return cheerio.load(body);
  }
};

rp(bread).then(($) => {
  console.log($('.product__summary').text());
}).catch((err) => {
  console.log(err);
});

我要获取产品描述: screenshot of this 页面,但我总是返回一个空字符串。我在这里做错了什么?

谢谢!

为了找到答案,让我们尝试以两种不同的方式检查页面(在 Chrome 上):

1.Right点击->查看页面源码 2.ClickF12

你会注意到class "product__summary"只有在我们点击F12的时候才会存在,那是因为只有在浏览器执行页面的脚本后,才会出现这个class的元素。

Cheerio 不在页面上执行脚本,因此您看不到这个特定的 class。

您可以使用更轻的Selenium, which is pretty heavy to run on scale, or jsdom

祝你好运!