Wage vs Salary in UA

node v14.20.1
version: master
endpointsharetweet
Моя жалкая попытка в статистику
const _ = require('lodash') const a = require('axios') const cheerio = require('cheerio') const moment = require('moment') const wrapPlotly = require('runkit-plotly'); const makeChart = wrapPlotly((data, elem, Plotly) => { Plotly.newPlot(elem, data); });
class WageminExtractor { async get() { return this.parse((await a.get('https://index.minfin.com.ua/labour/wagemin/')).data); } parse(html) { const $ = cheerio.load(html); const tables = $('table'); const table = tables.get(tables.length-1); const rows = $(table).find('tr'); const [,...dataRows] = Array.from(rows); const data = dataRows.map(row => { const $row = $(row); const fromTo = $row.find('td:nth-child(1)').text(); const wage = $row.find('td:nth-child(2)').text(); const [, from, to] = fromTo.match(/.+([\d\.]{10}).+([\d\.]{10})/) || []; if(!from || !to || !wage) { return null; } return { from: moment(from, 'DD.MM.YYYY'), to: moment(to, 'DD.MM.YYYY'), wage: parseInt(wage), } }).filter(Boolean); return data; } }
class SalaryExtractor { async get() { return this.parse((await a.get('https://uk.wikipedia.org/wiki/%D0%A1%D0%B5%D1%80%D0%B5%D0%B4%D0%BD%D1%8F_%D0%B7%D0%B0%D1%80%D0%BE%D0%B1%D1%96%D1%82%D0%BD%D0%B0_%D0%BF%D0%BB%D0%B0%D1%82%D0%B0_%D0%B2_%D0%A3%D0%BA%D1%80%D0%B0%D1%97%D0%BD%D1%96')).data); } parse(html) { const $ = cheerio.load(html); const tables = $('table'); const table = tables.get(tables.length-1); const rows = $(table).find('tr'); const [,...dataRows] = Array.from(rows); const data = dataRows.map(row => { const $row = $(row); const year = $row.find('td:nth-child(1)').text(); const salary = $row.find('td:nth-child(2)').text(); if(parseInt(year)<1996) { return null; } return { year:moment(parseInt(year),'YYYY'), salary:parseInt(salary), } }).filter(Boolean); return data; } }
const we = new WageminExtractor(); const wages = (await we.get()) const wageAxes = _(wages) .map(({to,wage})=>[ to.toDate(), wage ]) .unzip() .value() ;
const se = new SalaryExtractor(); const salaries = (await se.get()) const salaryAxes = _(salaries) .map(({year,salary})=>[ year.toDate(), salary ]) .unzip() .value() ;
makeChart( [{ x: wageAxes[0], y: wageAxes[1], mode: 'lines', type: 'scatter', name: 'Прожиточный минимум' },{ x: salaryAxes[0], y: salaryAxes[1], mode: 'lines', type: 'scatter', name: 'Средняя заработная плата' }], { width: 700, height: 500 } );
Loading…

no comments

    sign in to comment