Puppeteer
Scraping Browserは、動的なウェブサイトからデータ抽出のプロセスを簡素化するために設計された、高性能なサーバーレスプラットフォームを提供します。Puppeteerとのシームレスな統合により、開発者は、専用のサーバーを必要とせずに、ヘッドレスブラウザを実行、管理、監視することができ、効率的なウェブ自動化とデータ収集を実現します。
必要なライブラリのインストール
まず、既存のブラウザインスタンスに接続するために設計された、軽量バージョンのPuppeteerであるpuppeteer-core
をインストールします。
npm install puppeteer-core
Scraping Browserに接続するためのコードの記述
Puppeteerコードで、次のようにScraping Browserに接続します。
const { Puppeteer } = require('@scrapeless-ai/sdk');
(async () => {
const browser = await Puppeteer.connect({
session_name: 'sdk_test',
session_ttl: 180,
proxy_country: 'US',
session_recording: 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操作をいくつか紹介します。
- ナビゲーションとページコンテンツの抽出
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();
- スクリーンショットの撮影
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();
- カスタムコードの実行
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();
- マウスのクリックをシミュレートします。
const { createPuppeteerCDPSession } = require('@scrapeless-ai/sdk');
// ... 上記のようにScraping Browserに接続します
const cdpSession = await createPuppeteerCDPSession(page);
await cdpSession.realClick('button[type="submit"]');
- キーボード入力をシミュレートします。
const { createPuppeteerCDPSession } = require('@scrapeless-ai/sdk');
// ... 上記のようにScraping Browserに接続します
const cdpSession = await createPuppeteerCDPSession(page);
await cdpSession.realFill('#login-email', 'scrapeless@gmail.com');
- Scrapeless Agentを使用して現在のページURLを取得します
const { createPuppeteerCDPSession } = require('@scrapeless-ai/sdk');
// ... 上記のようにScraping Browserに接続します
const cdpSession = await createPuppeteerCDPSession(page);
const { error, liveURL } = await cdpSession.liveURL();
- 画像認証を解決します
const { createPuppeteerCDPSession } = require('@scrapeless-ai/sdk');
// ... 上記のようにScraping Browserに接続します
const cdpSession = await createPuppeteerCDPSession(page);
await cdpSession.imageToText({
imageSelector: '.captcha__image',
inputSelector: 'input[name="captcha"]',
timeout: 30000,
});
- 自動的な認証の解決を無効にします
const { createPuppeteerCDPSession } = require('@scrapeless-ai/sdk');
// ... 上記のようにScraping Browserに接続します
const cdpSession = await createPuppeteerCDPSession(page);
await cdpSession.disableCaptchaAutoSolve();
- 指定されたオプションで認証を手動で解決します
const { createPuppeteerCDPSession } = require('@scrapeless-ai/sdk');
// ... 上記のようにScraping Browserに接続します
const cdpSession = await createPuppeteerCDPSession(page);
await cdpSession.solveCaptcha();
- ページで認証が検出されるのを待ちます
const { createPuppeteerCDPSession } = require('@scrapeless-ai/sdk');
// ... 上記のようにScraping Browserに接続します
const cdpSession = await createPuppeteerCDPSession(page);
await cdpSession.waitCaptchaDetected({ timeout: 30000 });
- 認証が解決されるのを待ちます(成功または失敗)
const { createPuppeteerCDPSession } = require('@scrapeless-ai/sdk');
// ... 上記のようにScraping Browserに接続します
const cdpSession = await createPuppeteerCDPSession(page);
await cdpSession.waitCaptchaSolved({ timeout: 30000 });