# PuppeteerDemo **Repository Path**: xiaoxia_dyh/PuppeteerDemo ## Basic Information - **Project Name**: PuppeteerDemo - **Description**: pupppeter实现的对百度,网页新闻的爬虫 - **Primary Language**: JavaScript - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 4 - **Forks**: 0 - **Created**: 2018-02-02 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # PuppeteerDemo pupppeter实现的对百度,网页新闻的爬虫 ; 并可以用node搭建一个小型的服务。
做成产品的话可以实现一个 文章的采集中心 # 初始化项目 npm install 如果你没有下载chromium的话,可以npm i puppeteer一下,让他自己去下载适合的版本。 ## 主要代码解释 puppeteer是一个无界面浏览器,可以模拟用户的操作,以下是模拟用户登录百度的账号,但是写完发现百度有验证码,尴尬了... ## 代码 --- class LoginBaiDu{ constructor() { this.url = 'https://www.baidu.com/'; this.init(); } async init(){ console.log('正在启动浏览器...'); this.browser = await puppeteer.launch({headless:false}); console.log('正在打开新页面...'); this.page = await this.browser.newPage(); await this.openBaiDu(this.url); console.log('正在关闭浏览器...'); await timeout(delay); await this.browser.close(); } async openBaiDu(url){ await timeout(delay); let page = this.page; await page.goto(url); try{ await timeout(delay); var login = await page.$('#u1 .lb'); console.log('正在获取登录节点---------------'+login); await login.click(); await timeout(1000); var userNameLogin = await page.$('#TANGRAM__PSP_10__footerULoginBtn'); await userNameLogin.click(); await timeout(1000) await page.type('[name=userName]','来生只为你心动',{delay:200}); await timeout(500); // 密码就没填 ^_^ await page.type('[name=password]','***',{delay:200}); await timeout(1000) // 点击登录 console.log('点击登录百度...'); var commit = await page.$('#TANGRAM__PSP_10__submit'); await commit.click(); await timeout(2000) } catch(e){ console.log("执行出错......."+e); await page.screenshot({path:'./err.png',type: 'png'}); } } } //到网易体育的首页,抓取网易推荐的新闻的a链接列表 var listMap = await page.evaluate(() => { var alist = [...document.querySelectorAll('.topnews_news ul li a')]; return alist.map((el) => { return { href: el.href, title: el.innerText } }); }); // 遍历这些a链接,去文章详情页 把文章的标题,来源和内容抓取出来 for(var i=0;i el.innerText); var source = await page.$eval('#ne_article_source',el => el.innerText); var contentP = await page.evaluate(() => { var pList = [...document.querySelectorAll('#endText p')]; return pList.map(el => { return { p:el.innerHTML }; }); }); console.log('----------load content start-----------------------'); console.log(title); console.log(source); console.log(contentP); console.log('---------load content end--------------------------'); // 将他们保存在本地文件中,其实一般 要做一个新闻类的产品的话,把文章解析出来直接存在自己的数据库就可以了 // 然后app端取自己库里面的最新文章 var cont = title+"\r\n"+source+"\r\n"+contentP; fs.writeFile("./"+title+".txt", cont,{flag:'a',encoding:'utf-8',mode:'0666'}, function(err) { if(err) { return console.log(err); } }); } catch(e){ console.log(e); } } --- [puppeteer的api文档](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#mouseclickx-y-options) 这是英文的,本人英语不好,能看懂,翻译不了 不过这里有个大神翻译了很多的api,一般的需求都能满足了。[博客地址](http://blog.csdn.net/u010142437/article/details/79136182)