UNPKG

wordreference-definition-api

Version:

Scrap word definitions from http://www.wordreference.com

39 lines (37 loc) 1.49 kB
const { textContent } = require('../utils/jquery.js') const cheerio = require('cheerio') class Scrap { HTML(html) { let $ = cheerio.load(html) let rhs = $('.entryRH') let currentClazzName = '' let data = [] const getKey = () => data.length rhs.each((rhKey, rh) => { let clazz = $(rh).find('.rh_empos, .rh_pos') let ols = $(rh).find('ol') clazz.each((clazzKey, clazzEL) => { currentClazzName = $(clazzEL).text() //each grammar class has only one ol's //if you remove it, everything will get desynchronized if ((clazzKey + 1) > ols.length) { return; } let key = getKey() data[key] = { class: '', defs: [{ def: '', examples: [] }] } data[key].class = currentClazzName let d = $(ols[clazzKey]).find('.rh_def') d.each((defKey, defEL) => { defEL = $(defEL) data[key].defs[defKey] = { def: '', examples: [] } data[key].defs[defKey].def = textContent(defEL, $) defEL.find('.rh_ex').each((_, exampleEL) => { data[key].defs[defKey].examples.push($(exampleEL).text()) }) }) }) }) return data } } module.exports = new Scrap