1-1 简单实用
const puppeteer = require("puppeteer");
const url = "http://www.kuwo.cn/search/list?key=你";
(async ()=>{
const browser = await puppeteer.launch({
headless:false, //模拟浏览器行为
defaultViewport:null, //可视区域最大化
args: ['--start-maximized'], //浏览器窗口最大化
ignoreDefaultArgs:['--enable-automation--'] //忽略验证
});
const page = await browser.newPage();
await page.goto(url)
await page.waitFor(9000) //页面等待9s
await page.screenshot({
path:"douban.png"
})
await browser.close()
})();
1-2 爬虫 puppeteer+cheerio爬取酷我
const puppeteer = require("puppeteer");
const cheerio = require("cheerio")
const url = "http://www.kuwo.cn/search/list?key=浪子闲话";
(async () => {
const browser = await puppeteer.launch({
headless: false,
args: ['--start-maximized'],
defaultViewport: null
});
const page = await browser.newPage();
await page.goto(url, {
waitUntil: 'networkidle2'
})
await page.waitForTimeout(3000)
await page.hover(".search_list")
var content = await page.content();
var $ = cheerio.load(content,{
decodeEntities:false
})
var title = $(".song_name").find(".name").html()
console.log(title)
await browser.close()
})();
1-3 evaluate
const puppeteer = require('puppeteer');
(async () => {
/* 1.新建一个浏览器的实例 */
const browser = await puppeteer.launch({
headless:false,
defaultViewport:null
});
/* 2.新建一个页面 */
const page = await browser.newPage();
/* 3.到达某个页面 */
await page.goto('https://movie.douban.com/top250');
/* 4.在页面停顿3s */
await page.waitForTimeout(1000)
/* 5.evaluate可以获取页面内容 */
var res = await page.evaluate(()=>{
var $ = window.jQuery;
var content = $("#content h1").html();
return content
})
console.log(res)
await browser.close();
})();