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');
  }
});

 

上一篇:MYSQL 错误日志


下一篇:Cannot read property 'parent' of undefined cheerio