puppeteer获取课程并写入json

懵懂的女人 提交于 2019-11-27 06:00:36
const puppeteer = require('puppeteer');
const fs = require('fs');
const imoocUrl = 'https://www.imooc.com/course/list?c=fe';
;(async () => {
    const browser = await puppeteer.launch({
        headless : false,
        defaultViewport:{ width: 1920, height: 1080 }});
    const page = await browser.newPage();
    console.log('start open url:',imoocUrl);
    await page.goto(imoocUrl);
    
    //操作数据
    console.log('operate dom by console');
    const result = await page.evaluate(() => {
        let $ = window.$;
        let data = [];
        let courseList = $('.moco-course-list').find('.course-card-container');
        if(courseList.length > 1){
            courseList.each((index,item) => {
                let item_a = $(item).find('a');
                let tags = [];
                let labels = item_a.find('.course-label label');
                if(labels.length > 0){
                    labels.each((index,item) => {
                        tags.push($(item).text());
                    })
                }
                let content = item_a.find('.course-card-content');
                let title = content.find('.course-card-name').text();
                let card = content.find('.course-card-info span');
                let level = $(card[0]).text();
                let desc = content.find('.course-card-desc').text();
                data.push({
                    title,
                    tags: tags.join(','),
                    level,
                    desc
                });
            })
        }
        return data;
    });

    await browser.close();
    console.log('打印数据','\n',result);

    //将数据写入到文件中,通过fs模块
    let apiData = {
        data: result,
        code: 0,
        message: 'success'
    }
    fs.writeFile('course-list.json',JSON.stringify(apiData,null,'\t'), function(err, data) {
        if (err) {
            throw err;
        }});
})();
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!