iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 17
0
AI & Data

Puppeteer 簡單快速建立自己的 Nodejs 爬蟲系列 第 17

Day 17 Puppeteer API 介紹 - class: Page method part 3

  • 分享至 

  • xImage
  •  
const puppeteer = require('puppeteer');

puppeteer.launch().then(async browser => {
  const page = await browser.newPage();
  await page.setRequestInterception(true);
  page.on('request', interceptedRequest => {
    if (interceptedRequest.url().endsWith('.png') || interceptedRequest.url().endsWith('.jpg'))
      interceptedRequest.abort();
    else
      interceptedRequest.continue();
  });
  await page.goto('https://example.com');
  await browser.close();
});
同時執行
const [response] = await Promise.all([
  page.waitForNavigation(), // The promise resolves after navigation has finished
  page.click('a.my-link'), // Clicking the link will indirectly cause a navigation
]);
const firstRequest = await page.waitForRequest('http://example.com/resource');
const finalRequest = await page.waitForRequest(request => request.url() === 'http://example.com' && request.method() === 'GET');
return firstRequest.url();
const firstResponse = await page.waitForResponse('https://example.com/resource');
const finalResponse = await page.waitForResponse(response => response.url() === 'https://example.com' && response.status() === 200);
return finalResponse.ok();
const puppeteer = require('puppeteer');

puppeteer.launch().then(async browser => {
  const page = await browser.newPage();
  let currentURL;
  //等待img tag出現
  page
    .waitForSelector('img')
    .then(() => console.log('First URL with image: ' + currentURL));
  //非同步 每次都會等待tag第一次出現
  for (currentURL of ['https://example.com', 'https://google.com', 'https://bbc.com'])
    await page.goto(currentURL);
  await browser.close();
});

上一篇
Day 16 Puppeteer API 介紹 - class: Page method part 2
下一篇
Day 18 Puppeteer API 介紹 - class: Frame, ConsoleMessage
系列文
Puppeteer 簡單快速建立自己的 Nodejs 爬蟲25
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言