nodejs 爬热搜
const request = require('request'); const cheerio = require('cheerio'); const fs = require('fs'); const url = 'https://s.weibo.com/top/summary'; request(url, (err, resp, body) => { if (err) { console.log(err); } else { const $ = cheerio.load(body); const news = []; $(".td-02 a").each(function(i, elem) { let newItem = { text: $(this).text(), url: $(this).attr('href') }; news[i] = newItem; }); fs.writeFile('output.json', JSON.stringify(news), function (err) { if (err) { console.log(err); } else { console.log('ok.'); } }); console.log('end'); } });
掌控自己,掌控命运