|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const cheerio = require('cheerio'); |
| 5 | +const got = require('got'); |
| 6 | +const each = require('async-each'); |
| 7 | + |
| 8 | +const parentUrl = 'http://www.w3schools.com/cssref/'; |
| 9 | +const childUrl = (url) => `${parentUrl}${url}`; |
| 10 | +const results = []; |
| 11 | + |
| 12 | +const getPropValues = (url) => { |
| 13 | + return new Promise((resolve, reject) => { |
| 14 | + got(childUrl(url)) |
| 15 | + .then(response => { |
| 16 | + let $child = cheerio.load(response.body); |
| 17 | + let $rows = $child('.w3-table-all tr'); |
| 18 | + let results = []; |
| 19 | + |
| 20 | + $rows.each((i, row) => { |
| 21 | + if (i > 0) { |
| 22 | + let $cols = $child(row).find('td'); |
| 23 | + let value = $cols.eq(0).text(); |
| 24 | + results.push(value); |
| 25 | + } |
| 26 | + }); |
| 27 | + resolve(results); |
| 28 | + }) |
| 29 | + .catch(reject); |
| 30 | + }); |
| 31 | +}; |
| 32 | + |
| 33 | +const saveResults = () => { |
| 34 | + fs.writeFile('css-properties-values.json', JSON.stringify(results)); |
| 35 | +} |
| 36 | + |
| 37 | +got(parentUrl) |
| 38 | + .then(response => { |
| 39 | + let $parent = cheerio.load(response.body); |
| 40 | + let $rows = $parent('.w3-table-all tr'); |
| 41 | + |
| 42 | + each($rows.toArray(), (row, next) => { |
| 43 | + let $cols = $parent(row).find('td'); |
| 44 | + let property = $cols.eq(0).text(); |
| 45 | + let childUrl = $parent(row).find('a').attr('href'); |
| 46 | + |
| 47 | + // no prop, skip |
| 48 | + if (!property.length) { |
| 49 | + next(); |
| 50 | + } else { |
| 51 | + if (childUrl) { |
| 52 | + // get values of property |
| 53 | + getPropValues(childUrl) |
| 54 | + .then((values) => { |
| 55 | + results.push({ |
| 56 | + property, |
| 57 | + values |
| 58 | + }); |
| 59 | + next(); |
| 60 | + }) |
| 61 | + .catch((err) => console.log(err)); |
| 62 | + } else { |
| 63 | + // no values, push anyways |
| 64 | + results.push({ |
| 65 | + property, |
| 66 | + values: null |
| 67 | + }); |
| 68 | + next(); |
| 69 | + } |
| 70 | + } |
| 71 | + }, saveResults); |
| 72 | + }) |
| 73 | + .catch(error => { |
| 74 | + console.log(error.response.body); |
| 75 | + }); |
0 commit comments