Advertisement
NusanTech

Populasi Manusia

Jun 14th, 2025
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 1.35 KB | Source Code | 0 0
  1. const axios = require('axios');
  2. const cheerio = require('cheerio');
  3.  
  4. async function skrapepopulasimanusia() {
  5.     try {
  6.         const response = await axios.get('https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_population',
  7.         {
  8.             headers: {
  9.                 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36'
  10.             }
  11.         }
  12.     );
  13.  
  14.     const $ = cheerio.load(response.data);
  15.     const result = [];
  16.     const table = $('table.wikitable.sortable').first();
  17.  
  18.     table.find('tbody tr').each((i, el) => {
  19.         const td = $(el).find('td');
  20.  
  21.         if (td.length >= 6) {
  22.             const country = $(td[0]).text().trim().replace(/\[\d+\]/g, '');
  23.             const population = $(td[1]).text().trim().replace(/,/g, '');
  24.             const percentWorld = $(td[2]).text().trim();
  25.             const date = $(td[3]).text().trim();
  26.             const source = $(td[4]).text().trim();
  27.  
  28.             result.push({
  29.                 country,
  30.                 population,
  31.                 percentWorld,
  32.                 date,
  33.                 source
  34.             });
  35.         }
  36.     });
  37.  
  38.         console.log(result);
  39.     } catch (err) {
  40.         console.error('Error we, error aman kan ada catch : ', err.message);
  41.     }
  42. }
  43.  
  44. skrapepopulasimanusia();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement