Skip to content

爬虫

cheerio

// 抓取热点新闻
const https = require('https')
const cheerio = require('cheerio')
const fs = require('fs')


https.get('https://top.baidu.com/board?platform=pc&sa=pcindex_entry', function (res) {
    let html = ''
    res.on('data', function (chunk) {
        html += chunk
    })

    res.on('end', function () {
        const $ = cheerio.load(html)
        let allFiles = []

        $('.list_1EDla > a').each(function () {
            const title = $('.c-single-text-ellipsis', this).html().trim()
            allFiles.push({
                title: title
            })
        })

        fs.writeFile('./files.json', JSON.stringify(allFiles), function (err, data) {
            if (err) {
                throw err
            }
            console.log('文件保存成功')
        })
    })
})