ドキュメントブラウザーと CrawlAgent BrowserPuppeteer で接続

Puppeteer

Scraping Browser は、動的ウェブサイトからデータを抽出するプロセスを簡素化するために設計された高性能なサーバーレスプラットフォームを提供します。Puppeteer とのシームレスな統合により、開発者は専用サーバーを必要とせずにヘッドレスブラウザを実行、管理、監視でき、効率的なウェブ自動化とデータ収集が可能になります。

必要なライブラリのインストール

まず、既存のブラウザインスタンスに接続するように設計された軽量版の puppeteer-coreをインストールします:Puppeteer

npm install puppeteer-core

Scraping Browser に接続するコードの作成

Puppeteer のコード内で、以下を使用して Scraping Browser に接続します:

const { Puppeteer } = require('@scrapeless-ai/sdk');
 
(async () => {
    const browser = await Puppeteer.connect({
        apiKey: 'Your API key',
        sessionName: 'sdk_test',
        sessionTTL: 180,
        proxyCountry: 'US',
        sessionRecording: true,
        defaultViewport: null
    });
 
    const page = await browser.newPage();
    await page.goto('https://www.scrapeless.com');
    console.log(await page.title());
    await browser.close();
})();

これにより、スケーラビリティ、IPローテーション、グローバルアクセスなど、Scraping Browser のインフラストラクチャを活用できます。

実用例

Scraping Browser を統合した後によく使用される Puppeteer の操作を以下に示します:

  1. ナビゲーションとページコンテンツの抽出
const page = await browser.newPage();
await page.goto('https://www.example.com');
console.log(await page.title());
const html = await page.content();
console.log(html);
await browser.close();
 
  1. スクリーンショットの取得
const page = await browser.newPage();
await page.goto('https://www.example.com');
await page.screenshot({ path: 'example.png' });
console.log('Screenshot saved as example.png');
await browser.close();
 
  1. カスタムコードの実行
const page = await browser.newPage();
await page.goto('https://www.example.com');
const result = await page.evaluate(() => document.title);
console.log('Page title:', result);
await browser.close();
 
  1. マウスクリックをシミュレートします。
const { createPuppeteerCDPSession } = require('@scrapeless-ai/sdk');
// ... connect to Scraping Browser as shown above
const cdpSession = await createPuppeteerCDPSession(page);
await cdpSession.realClick('button[type="submit"]');
  1. キーボード入力のシミュレーション。
const { createPuppeteerCDPSession } = require('@scrapeless-ai/sdk');
// ... connect to Scraping Browser as shown above
const cdpSession = await createPuppeteerCDPSession(page);
await cdpSession.realFill('#login-email', 'scrapeless@gmail.com');
  1. Scrapeless Agent を使用して現在のページの URL を取得
const { createPuppeteerCDPSession } = require('@scrapeless-ai/sdk');
// ... connect to Scraping Browser as shown above
const cdpSession = await createPuppeteerCDPSession(page);
const { error, liveURL } = await cdpSession.liveURL();
  1. 画像キャプチャの解決
const { createPuppeteerCDPSession } = require('@scrapeless-ai/sdk');
// ... connect to Scraping Browser as shown above
const cdpSession = await createPuppeteerCDPSession(page);
await cdpSession.imageToText({
  imageSelector: '.captcha__image',
  inputSelector: 'input[name="captcha"]',
  timeout: 30000,
});
  1. 自動キャプチャ解読を無効化
const { createPuppeteerCDPSession } = require('@scrapeless-ai/sdk');
// ... connect to Scraping Browser as shown above
const cdpSession = await createPuppeteerCDPSession(page);
await cdpSession.disableCaptchaAutoSolve();
  1. 指定したオプションでキャプチャを手動で解読
const { createPuppeteerCDPSession } = require('@scrapeless-ai/sdk');
// ... connect to Scraping Browser as shown above
const cdpSession = await createPuppeteerCDPSession(page);
await cdpSession.solveCaptcha();
  1. ページ上でキャプチャが検出されるのを待機
const { createPuppeteerCDPSession } = require('@scrapeless-ai/sdk');
// ... connect to Scraping Browser as shown above
const cdpSession = await createPuppeteerCDPSession(page);
await cdpSession.waitCaptchaDetected({ timeout: 30000 });
  1. キャプチャが解読されるのを待機(成功または失敗のいずれか)
const { createPuppeteerCDPSession } = require('@scrapeless-ai/sdk');
// ... connect to Scraping Browser as shown above
const cdpSession = await createPuppeteerCDPSession(page);
await cdpSession.waitCaptchaSolved({ timeout: 30000 });