Path: blob/master/node_modules/@bochilteam/scraper/lib/esm/others/KBBI.js
1126 views
import got from 'got';1import cheerio from 'cheerio';2import { ScraperError } from '../utils.js';3/**4* p = Partikel: kelas kata yang meliputi kata depan, kata sambung, kata seru, kata sandang, ucapan salam5*6* n = Nomina: kata benda7*/8export default async function kbbi(words) {9const html = await got(`https://kbbi.kemdikbud.go.id/entri/${encodeURIComponent(words)}`).text();10const $ = cheerio.load(html);11const isExist = !/tidak ditemukan/i.test($('body > div.container.body-content > h4[style="color:red"]').text());12if (!isExist)13throw new ScraperError(`${words} does not exist!`);14const results = [];15let isContent = false;16let lastTitle;17$('body > div.container.body-content').children().each((_, el) => {18const tag = el.tagName;19const elem = $(el);20if (tag === 'hr')21isContent = !isContent && !Object.keys(results).length;22if (tag === 'h2' && isContent) {23const index = elem.find('sup').text().trim();24const title = elem.text().trim();25results.push({26index: parseInt(index),27title,28means: []29});30lastTitle = title;31}32if ((tag === 'ol' || tag === 'ul') && isContent && lastTitle) {33elem.find('li').each((_, el) => {34const li = $(el).text().trim();35const index = results.findIndex(({ title }) => title === lastTitle);36if (index !== -1)37results[index].means.push(li);38else39console.log(li, lastTitle);40});41lastTitle = '';42}43});44return results;45}46//# sourceMappingURL=KBBI.js.map4748