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;
}});
})();
来源:https://blog.csdn.net/qq_18547449/article/details/99478278