编程语言
首页 > 编程语言> > nodejs 爬热搜

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

 

标签:fs,console,log,err,nodejs,cheerio,const
来源: https://www.cnblogs.com/qiuxd/p/13168194.html