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
| async function getMovieData(url) { const { data: movieHtml } = await axios.get(url) const _$ = cheerio.load(movieHtml) const info = _$('#info').html().replace(/\s/g, '') const movie = { imdb: info.match(/IMDb.*?<br>/)?.[0].replace(/<.*?>|IMDb|:/g, ''), name: _$('#content > h1 > span:nth-child(1)').text().split(' ')?.[0], imgUrl: _$('#mainpic > a > img').attr('src'), detail: _$('#link-report-intra > span:nth-child(1)').text(), } let keyStr = '' let valueStr = '' for (const key in movie) { movie[key] = movie[key].replace(/\s/g, '').replace(/'/g, '') keyStr += key + ',' valueStr += movie[key] + ',' } const sqlStr = 'insert into douban250 (' + keyStr.slice(0, -1) + ') values (' + valueStr.slice(0, -1) + ')' connection.query(sqlStr, function (err, results, fields) { if (err) { console.log('新增出错: 《' + movie.name + '》\n' + err) console.log(sqlStr) return } }) }
function sleep(ms) { return new Promise((resolve) => setTimeout(resolve, ms)) }
|