简体   繁体   中英

Access specific div using cheerio

I am trying to use cheerio in order to access one string of characters that is within a div in HTML.

This is what the div looks like on the website:

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

I have tried accessing the div using this code here:

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

When I run the JS it seems to work halfway... I get an empty line on the console where the text should be.

Thanks for the answers in advance!

Apparently, Facebook returns different payloads depending on the user agent header.

Try the following:

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());
});

Although the above works, it's probably an ephemeral solutions as it's possible Facebook changes the _6wae class frequently.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM