taufik
taufik

Reputation: 269

how to get data with cheerio? when i see in page source data is empty, but when i see in inspect element i see the data

i hope you care. I have troble when scraping data in another website. this my case, when i see in page source data is empty, but when i see in inspect element i see the data. if you not understand see these image, first in inspect element and second in view page source

enter image description here

enter image description here

this is my code

const request = require('request');
const cheerio = require('cheerio');
const url = "https://pikobar.jabarprov.go.id/"

request(url, (error, response, html) => {
   let $ = cheerio.load(html);
   $('b').each((i, element) => {
      let omg = $(element).text();
      console.log(omg);
   });
});

this is my result enter image description here

i want text 388

Upvotes: 0

Views: 725

Answers (1)

ariel
ariel

Reputation: 16150

If you see the document source (Ctrl+U), this element is really empty. That's because it's being filled afterwards, by a Ajax request. If you go to Network tab and reload, you will see all files being transferred. The url you are looking for is https://covid19-public.digitalservice.id/api/v1/rekapitulasi/jabar?level=prov which have the data you are looking for.

fetch("https://covid19-public.digitalservice.id/api/v1/rekapitulasi/jabar?level=prov")
  .then(r => r.json())
  .then(d => console.log(d.data.content.positif))

Upvotes: 1

Related Questions