Puppeteer - some case

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 iframe
  await 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 iframe
  const 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();

  // 匹配page
  const 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();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值