const puppeteer = require("puppeteer");
const fs = require('fs');
(async()=>{
try{
const browser = await puppeteer.launch({
headless:true,
args: [
'–disable-gpu', // GPU硬件加速
'–disable-dev-shm-usage', // 创建临时文件共享内存
'–disable-setuid-sandbox', // uid沙盒
'–no-first-run', // 没有设置首页。在启动的时候,就会打开一个空白页面。
'–no-sandbox', // 沙盒模式
'–no-zygote',
'–single-process' // 单进程运行
]
});
const page = await browser.newPage();
await page.goto("https://www.baidu.com/",{
'timeout': 0 //无限大
});
await page.waitFor(3000);
let body = await page.evaluate(() => {
let top = [...document.querySelectorAll('#s-top-left a')];
let buttom = [...document.querySelectorAll('.s-hotsearch-content li a')];
let buttom_layer = [...document.querySelectorAll('.s-bottom-layer-content p a')];
let title = document.querySelector('.title-text').innerText;
return {
title:title,
buttom:buttom_layer.map((b)=>{
return {
name: b.text
}
}),
top:top.map((a)=>{
return {
name: a.text
}
}),
hot_list:buttom.map((c)=>{
return {
name: c.text
}
})};
});
fs.writeFile('./json/course-list.json',JSON.stringify(body,null,'\t'),function(err, data) {
if (err) {
throw err;
}});
console.log(body);
browser.close();
}catch(err){
console.log(`this is the ${err}`);
}
})();
{
title: '百度热榜',
buttom: [
{ name: '设为首页' },
{ name: '关于百度' },
{ name: 'About Baidu' },
{ name: '百度营销' },
{ name: '使用百度前必读' },
{ name: '意见反馈' },
{ name: '帮助中心' },
{ name: '京公网安备11000002000001号' },
{ name: '京ICP证030173号' }
],
top: [
{ name: '新闻' },
{ name: 'hao123' },
{ name: '地图' },
{ name: '直播' },
{ name: '视频' },
{ name: '贴吧' },
{ name: '学术' },
{ name: '更多' },
{ name: '网盘' },
{ name: '知道' },
{ name: '百科' },
{ name: '图片' },
{ name: '宝宝知道' },
{ name: '文库' },
{ name: '经验' },
{ name: '音乐' },
{ name: '查看全部百度产品 >' }
],
hot_list: [
{ name: '1土耳其召见中国大使 中方回应热' },
{ name: '4杀人犯纸面服刑15年 84人担责' },
{ name: '2备孕者可以接种新冠疫苗' },
{ name: '5好利来辟谣卷入西藏冒险王事件' },
{ name: '3女孩不堪养三个弟妹轻生 妇联介入' },
{ name: '6美驱逐舰穿航台海 东部战区回应' }
]
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210408002409997.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3N5eF8xOTkw,size_16,color_FFFFFF,t_70)