打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
利用puppeteer采集百度数据,并写入json文件

利用puppeteer采集百度数据,并写入json文件


代码篇,属于日常采集篇,后期会mongdb来存贮数据


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美驱逐舰穿航台海 ***回应' }
  ]}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
puppeteer学习(二)
把HTML转成PDF的4个方案及实现
在puppeteer和request之间互相传输cookies
利用 Lighthouse 與 Puppeteer 對全站網頁做效能檢測並產生報表 | Summer。桑莫。夏天
puppeteer基本用法(puppeteer教程)
利用puppeteer破解极验的滑动验证
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服