Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const axios = require('axios');
- const cheerio = require('cheerio');
- async function skrapepopulasimanusia() {
- try {
- const response = await axios.get('https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_population',
- {
- headers: {
- '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'
- }
- }
- );
- const $ = cheerio.load(response.data);
- const result = [];
- const table = $('table.wikitable.sortable').first();
- table.find('tbody tr').each((i, el) => {
- const td = $(el).find('td');
- if (td.length >= 6) {
- const country = $(td[0]).text().trim().replace(/\[\d+\]/g, '');
- const population = $(td[1]).text().trim().replace(/,/g, '');
- const percentWorld = $(td[2]).text().trim();
- const date = $(td[3]).text().trim();
- const source = $(td[4]).text().trim();
- result.push({
- country,
- population,
- percentWorld,
- date,
- source
- });
- }
- });
- console.log(result);
- } catch (err) {
- console.error('Error we, error aman kan ada catch : ', err.message);
- }
- }
- skrapepopulasimanusia();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement