Puppeteer

puppeteer 中文教程

bilibili video: puppeteer系列教程 - 对应 code

Promise 写法

const puppeteer = require('puppeteer');// Promise type
puppeteer.launch({headless: false}).then(browser => {browser.newPage().then(page => {page.goto('https://www.baidu.com/');})
} )

async + await 写法

const puppeteer = require('puppeteer');async function run() {const browser = await puppeteer.launch({headless: false});const page = await browser.newPage();await page.goto('https://www.baidu.com/');const input_area = await page.$('#kw');await input_area.type('Hello World');const search_btn = await page.$('#su');await search_btn.click();
}
run();

获取文本元素值

const puppeteer = require('puppeteer');async function run() {const browser = await puppeteer.launch({headless: false});const page = await browser.newPage();await page.goto('https://www.baidu.com/');const input_area = await page.$('#kw');await input_area.type('Hello World');const search_btn = await page.$('#su');await search_btn.click();await page.waitForSelector('div#content_left > div.result-op.c-container.xpath-log', {visible: true});let resText = await page.$eval('#content_left > div.result-op.c-container.xpath-log', ele => ele.innerText);console.log(resText);await page.close();
}
run();

百度文件上传操作 - some limit, unsuccess

const puppeteer = require('puppeteer');async function upload() {const browser = await puppeteer.launch({headless: false,ignoreDefaultArgs: ['--enable-automation'],defaultViewport: { width: 1200, height: 900 }});const page = await browser.newPage();await page.goto('https://www.baidu.com/');const searchBtn = await page.waitForSelector('span.soutu-btn');await searchBtn.click();const uploadPic = await page.waitForSelector('input.upload-pic');console.log('uploadPic', uploadPic);await uploadPic.uploadFile('D:\\picPick\\Image 001.png');// await page.close();
}upload();

处理多个元素

const puppeteer = require('puppeteer');async function jd() {const browser = await puppeteer.launch({headless: false,defaultViewport: { width: 1200, height: 900 }});const page = await browser.newPage();await page.goto('https://www.jd.com/');const searchBtn = await page.waitForSelector('input#key');await searchBtn.type('手机');await page.keyboard.press('Enter');const uploadPic = await page.waitForSelector('ul.gl-warp > li.gl-item');const list = await page.$$eval('ul.gl-warp > li.gl-item', eles => eles.map(ele => ele.innerText));console.log('list ==', list);// await page.close();
}jd();

切换iframe进行安居客登陆操作

const puppeteer = require('puppeteer');async function upload() {const browser = await puppeteer.launch({headless: false,defaultViewport: { width: 1200, height: 900 }});const page = await browser.newPage();await page.goto('https://login.anjuke.com/login/form');// switch iframeawait page.frames().map(frame => console.log(frame.url));const targetFrameUrl = 'https://login.anjuke.com/login/iframeform';const frame = await page.frames().find(frame => frame.url().includes(targetFrameUrl));const phone = await frame.waitForSelector('#phoneIpt');await phone.type('18382257465');const getVerificationCodeBtn = await frame.waitForSelector('#sendSmsBtn');await getVerificationCodeBtn.click();
}upload();

拖拽操作阿里云验证码

const puppeteer = require('puppeteer');async function aliyun() {const browser = await puppeteer.launch({headless: false,ignoreDefaultArgs: ['--enable-automation'],defaultViewport: { width: 1200, height: 900 }});const page = await browser.newPage();await page.goto('https://account.aliyun.com/register/register.html');// switch iframeconst frame = await page.frames().find(frame => frame.url().includes('https://passport.aliyun.com'));const span = await frame.waitForSelector('#nc_1_n1z');const spanInfo = span.boundingBox();console.log('spanInfo:', spanInfo);const div = await frame.waitForSelector('#nc_1_scale_text>span');const divInfo = div.boundingBox();console.log('divInfo:', divInfo);await page.mouse.move(spanInfo.x, spanInfo.y);await page.mouse.down();for(let i = 0; i <divInfo.width; i++) {await page.mouse.move(divInfo.x + i, divInfo.y);}await page.mouse.up();
}aliyun();

自动抓取one语句自动发一条微博

const puppeteer = require('puppeteer');async function run() {const browser = await puppeteer.launch({headless: false,defaultViewport: { width: 1200, height: 700 },ignoreDefaultArgs: ['--enable-automation'],slowMo: 200,args: ['--window-size=1200,700']});const page = await browser.newPage();await page.goto('http://wufazhuce.com/', { waitUntil: 'networkidle2' });const oneText = await page.$eval('div.fp-one-cita > a', ele => ele.innerText);console.log('oneText', oneText);await page.goto('https://weibo.com/login.php', { waitUntil: 'networkidle2' });// await page.waitForTimeout(2000);// await page.reload();const countInput = await page.waitForSelector('input#loginname');await countInput.click();await countInput.type('15002813090');const passwordInput = await page.waitForSelector('input[type="password"]');await passwordInput.click();await countInput.type('@zhj@hj@');const loginBtn = await page.waitForSelector('a[action-type="btn_submit"]');await loginBtn.click();const textarea = await page.waitForSelector('textarea[class="w_input"]');await textarea.click();await textarea.type(oneText);const publicBtn = await page.waitForSelector('a[node-type="submit"]');await publicBtn.click();
}run();

切换浏览器tab页

const puppeteer = require('puppeteer');async function run() {const browser = await puppeteer.launch({headless: false,defaultViewport: { width: 1200, height: 700 },ignoreDefaultArgs: ['--enable-automation'],slowMo: 200,args: ['--window-size=1200,700']});const page = await browser.newPage();await page.goto('http://music.taihe.com/', { waitUntil: 'networkidle2' });const switchBtn = await page.waitForSelector('div.tag-box-inside>a');await switchBtn.click();// 匹配pageconst targetPage = await browser.waitForTarget(t => t.url().includes('songlist'));const newPage = await targetPage.page();const listText = await newPage.$eval('div.tracklist-box', ele => ele.innerText);console.log('listText:', listText);
}
run();

Puppeteer - some case - 未完相关推荐

  1. ExtJs之Ext.grid.GridPanel(部分未完)

    今天在家休息,年假不用就作费啊. 看了几部香港老电影,陪爸爸看了勇士占奇才, 然后,测试了一下EXTJS未完的内容, 在京东上订了七本历史普及书,近两百块..:) 搞定. ? 1 2 3 4 5 6 ...

  2. python爬虫requests源码链家_python爬虫——爬取链家房价信息(未完待续)

    爬取链家房价信息(未完待续) items.py # -*- coding: utf-8 -*- # Define here the models for your scraped items # # ...

  3. Android自定义View之七色环颜色采集器: 续我未完的大学梦 !

    Android自定义View之七色环颜色采集器:续我未完的大学梦!! 一.前言. 在大学期间,看到机智云开源的这个rgb灯,蛮好奇的,这么漂亮的颜色采集,并且可以同步到设备rbg灯颜色,甚是不解!这个 ...

  4. 《今日简史》读书笔记(未完待续)

    <今日简史>读书笔记(未完待续) 这本书是尤瓦尔·赫拉利的简史三部曲的最后一本,前2本书是<未来简史>和<人类简史>.根据豆瓣上网友的评价,这本书是尤瓦尔·赫拉利写 ...

  5. [每周软件]:Cucumber:未完待续的原因

    2019独角兽企业重金招聘Python工程师标准>>> 本来这个计划是一周的 剩下未完的三篇才是核心 两篇源码分析,一篇总结+BDD分析,但是因为目前水平有限 源码追了一部分之后追丢 ...

  6. 创建型模式——Factory Method(未完)

    当对某个对象的实例化代码散布在整个项目中的时候,似乎你已经可以嗅到坏味道了,我们叫做"创建蔓延".除非你肯定这个对象的实例化方法永远不会改变,否则最后将"创建的知识搬迁到 ...

  7. linux引数列项目过长,Linux 命令个人总结====== 未完待续 个人认为比较重要

    Linux 命令个人总结====== 未完待续 man [功能说明]: 查看帮助 [语法格式]: man [123456789]命令.文件. [选项参数]: 数字"1"表示用户命令 ...

  8. CC2530学习路线-基础实验-串口通讯发送字符串(4 未完待续)

    目录 1. 前期预备知识 1.1 串口通讯电路图 1.2 实验相关寄存器 1.2 常用波特率设置 本章未完待续..... 原来写的文章已经丢失了,只能找到这一小部分,看什么时候有时间再补上. 1. 前 ...

  9. [教程] [承風雅傳HSU]用ES4封裝Win7---ES4 Win7封裝教程(未完待續)

    [教程] [承風雅傳HSU]用ES4封裝Win7---ES4 Win7封裝教程(未完待續) a10036it 发表于 2015-7-27 21:11:19 https://www.itsk.com/t ...

最新文章

  1. 修改属性使按钮处于无验证状态
  2. Asp.net中GridView使用详解(引)【转】
  3. 小米开源FALSR算法:快速精确轻量级的超分辨率模型
  4. CMU赵越:异常检测的算法、案例和落地
  5. python conrurrent
  6. 【IT笔试面试题整理】字符串的排列
  7. 解决SpringBoot使用Quartz无法注入Bean的问题
  8. 数字图像噪声_Python
  9. python爬取多页数据_python爬虫实现爬取同一个网站的多页数据代码实例
  10. Ubuntu14.04+RabbitMQ3.6.3+Golang的最佳实践
  11. 研发管理系统选型必读
  12. C#项目班级管理系统
  13. 前端-js网页特效(三)动画效果及原理
  14. 家庭财务软件的概要分析
  15. Freeradius认证
  16. 吉他 c大调第一把位
  17. 随机效应与固定效应面板数据回归
  18. 父级fixed_CSS3--改变固定定位(fixed)的父级定位元素
  19. python和ruby对比
  20. 真正的端到端超像素网络——Superpixel Segmentation with Fully Convolutional Networks(CVPR2020)

热门文章

  1. python爬虫实战-爬取小说
  2. 远程桌面、云办公与云游戏的多种解决方案(由简入深)
  3. 最新WIN10系统封装教程2019系列(三)——必要的系统调整
  4. mysql数据库特殊字符_mysql数据库存入特殊字符
  5. Trinamic电机驱动芯片 完美替代TI DRV型号大全
  6. gta 6 android,【图片】GTA安卓新版CLEO+全系列游戏资源+FLA6.0【gta安卓吧】_百度贴吧...
  7. stm32使用PWM时,关闭PWM引脚会出现高电平解决方案
  8. github.io地址
  9. 行业词库收集程序已经开源
  10. 索尼电视android屡次停止,索尼电视应用助手目前暂停使用怎么办 教你解决