1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| const fs = require("fs"); const os = require("os"); const { getContent } = require('./getContent.js')
async function getInfo(list, index, func, update_time) { let name = list[index]; let otherInfo = {}; try { otherInfo = await func(name).catch(() => { if (index < list.length - 1) { index += 1; getInfo(list, index, func, update_time); } }); } catch (error) { if (index < list.length - 1) { index += 1; getInfo(list, index, func, update_time); return; } } fs.appendFile("animals.txt", JSON.stringify(otherInfo) + os.EOL, (err) => { if (err) { throw err; } else { if (index < list.length - 1) { index += 1; getInfo(list, index, func, update_time); } } }); }
async function animalInfo(url) { return new Promise(async (resolve, reject) => { try { const $ = await getContent(url).catch(err => { reject() }) const list = ['birthday', 'character', 'mantra', 'hobby', 'style', 'color', 'vioce', 'ethnicity', 'motto', 'foreign_name'] const nodes = $(".box-poke-left .box-poke") const str = $(".box-poke-left .box-title-1").text() const name = str.substr(0, str.length -1) const sex = str.substr(-1) === '♂' ? '男' : '女' const image = $(".box-poke-right").find('img').attr('src') const info = { name, sex, image, } for (let i = 0; i < list.length; i++){ const attr = list[i] const text = nodes.eq(i).find('.box-font').text() if (attr === 'birthday') { info[attr] = text.replace('月', '-').replace('日', '') } else { info[attr] = text } } info.birth_month = info.birthday.split('-').shift() resolve(info) } catch (error) { reject(url + '出错啦') } }) }
async function getAnimals(update_time, url) { const $ = await getContent(url) const nodes = $("#CardSelectTr tbody tr") const animals = [] const LENGTH = nodes.length for (let i = 1; i < LENGTH; i++){ let $element = $(nodes[i]); if ($element.find('td').eq(0).find('a').text() !== '40pxString') { const url = $element.find('td').eq(0).find('a').attr('href').substr(9) animals.push(url) } }
fs.unlink('animals.txt', function (error) { if (error) { console.log(error); return false; } }) let index = 0 getInfo(animals, index, animalInfo, update_time) }
const now = new Date() const query = encodeURIComponent('小动物图鉴') getAnimals(now, query)
|