1

我正在尝试使用cheerio来访问HTML 中的一串字符div

这是div网站上的样子:

<div class="_6wae">Only 5 Hours Left/div>

我曾尝试在div此处使用此代码访问:

let $ = cheerio.load(html);
const info = $('._6wae');
console.log(info.text());

当我运行 JS 时,它似乎工作了一半......我在控制台上得到一个空行,文本应该在哪里。

感谢您提前回答!

4

1 回答 1

0

显然,Facebook 根据用户代理标头返回不同的有效负载。

尝试以下操作:

const cheerio = require('cheerio');
const request = require('request');
request({
  method: 'GET',
  url: 'https://www.facebook.com/donate/2486339634914015/',
  headers: {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:69.0) Gecko/20100101 Firefox/69.0'
  }
}, function (error, response, body) {
  const $ = cheerio.load(body);
  var element = $('._6wae');
  console.log(element.text());
});

尽管上述方法有效,但它可能是一个临时解决方案,因为 Facebook 可能会_6wae频繁更改课程。

于 2019-09-16T17:18:04.733 回答