/*
- @Author: your name
- @Date: 2022-12-25 09:35:25
- @LastEditTime: 2022-12-25 18:59:57
- @LastEditors: Please set LastEditors
- @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
- @FilePath: \puppeteer\src\picture.js
*/
// ,它提供了一个高级 API 来通过 DevTools 协议控制 Chromium 或 Chrome
const puppeteer = require('puppeteer');
const request = require("request");
const fs = require("fs");
const path = require("path");
// defaultViewport: null,
// args: ['--start-maximized']
//文本
// (async () => {
// const browser = await puppeteer.launch({
// headless:false,
// defaultViewport: {
// width:1920,
// height:1080
// }
// });
// const page = await browser.newPage();
// await page.goto('https://wwww.baidu.com');
// await page.screenshot({path: Date.now()+'example.png'});
// await browser.close();
// })();
//导出源码
// (async function () {
// const browser = await puppeteer.launch();
// const page = await browser.newPage();
// await page.goto("https://wwww.baidu.com");
// const html = await page.content();
// fs.writeFileSync(path.join(__dirname, "exportHtml.html"), html);
// await browser.close();
// })();
// 打印pdf https://zhuanlan.zhihu.com/p/524254998?utm_id=0
// (async () => {
// const browser = await puppeteer.launch();
// const page = await browser.newPage();
// await page.goto('https://wwww.baidu.com', {waitUntil: 'networkidle2'});
// await page.pdf({path: 'hn.pdf', format: 'A4'});
// await browser.close();
// })();
//表单
// (async () => {
// const browser = await puppeteer.launch({
// headless:false,
// defaultViewport:{
// width:1920,
// height:1080
// },
// args: ['--start-fullscreen']
// // args: ['--start-maximized']
// });
// const page = await browser.newPage();
// page.setViewport({
// width:1920,
// height:1080
// })
// await page.goto('https://wwww.baidu.com', {waitUntil: 'networkidle2'});
// // let passwordInput = page.kw')//document.querySelector
// await page.type("#kw", "哈士奇图片", { delay: 100 });
// await page.waitFor(2000)
// let pictureDog = await page.$('#content_left .result-op .c-container .c-title a')
// pictureDog.click()
// await browser.close();
// })();
// //爬取图片
// let pictureSrc=async () => {
// const browser = await puppeteer.launch({
// headless:false,
// defaultViewport:{
// width:1920,
// height:1080
// },
// args: ['--start-fullscreen']
// // args: ['--start-maximized']
// });
// const page = await browser.newPage();
// page.setViewport({
// width:1920,
// height:1080
// })
// await page.goto('https://image.baidu.com/search/index?tn=baiduimage&ct=201326592&lm=-1&cl=2&ie=gb18030&word=%B9%FE%CA%BF%C6%E6&fr=ala&ala=1&alatpl=normal&pos=0&dyTabStr=MCwzLDEsNiw0LDUsOCw3LDIsOQ%3D%3D')
// await page.waitFor(3000)
// const data = await page.evaluate( ()=> {
// //拿到图片节点
// let dom = document.querySelectorAll('#imgid img')
// console.log(dom)
// let arr = []
// for(let i=0;i<dom.length-1;i++){
// //去除base64格式
// if(!dom[i].src.includes('base64'))
// arr.push(dom[i].src)
// }
// //返回图片链接
// return arr
// })
// return data
// await browser.close();
// };
// pictureSrc().then((value) => {
// for(let i = 1; i < value.length-1; i++) {
// let imgUrl = value[i];
// let filename = ${new Date().getTime()}imgage${i}.png
;
// //在C盘下创建文件夹
// fs.mkdir(__dirname+'downloadpic',(e)=>{
// if(!e || (e&&e).code === 'EEXIST'){
// if(imgUrl)
// //保存在这个目录下
// request(imgUrl).pipe(fs.createWriteStream(__dirname+"downloadpic/" + filename));
// else
// console.log('地址为空')
// }
// else{
// console.log('err')
// }
// })
// }
// });
//服务端渲染 网络seo
// 抓取内容之后拦截
// page.request page.response
puppeteer.launch(conf.launchOption).then(async browser => {
const server = http.createServer();
server.on('request', function(request, response){
let url = (conf.useHttps ? 'https://' : 'http://') + request.headers.host + request.url;
let ip = getIp(request);
// 检测ip地址
if (checkIP(ip)) {
browser.newPage().then(async page => {
// 访问url
await page.goto(url);
let content= await page.content();
// 关闭页面,返回数据
await page.close();
response.end(content);
})
} else {
response.statusCode = 404;
response.end("404 NOT Found");
}
})
server.on('close', function(){
browser.close();
})
server.listen(3000, '127.0.0.1', function () {
console.log('puppeteer server listening on port 3000!');
});
});
/// 性能优化
(async () => {
const browser = await puppeteer.launch({
headless:false,
})
const page = await browser.newPage()
await page.goto('https://wwww.baidu.com')
const performanceTiming = JSON.parse(
await page.evaluate(() => JSON.stringify(window.peformance.timing))
);
console.log(performanceTiming);
})()
//测试浏览器插件
const CRX_PATH = '/path/to/crx/folder/';
puppeteer.launch({
headless: false, // extensions only supported in full chrome.
args: [
--disable-extensions-except=${CRX_PATH}
,
--load-extension=${CRX_PATH}
,
]
}).then(async browser => {
const page = await browser.newPage();
await page.evaluate("document.title = 'PuppeteerAgent'");
// ... do some testing ...
await browser.close();
});