Playwright Amazon Scraper: 製品&レビュー(Javascript)
Olivia Novak
Dev Intern · Leapcell

Web Automation and Data Collection with Playwright (Node.js Version)
Playwrightは、Chromium、Firefox、WebKitなどのブラウザをサポートする、ウェブページのテストと自動化のためのライブラリです。Microsoftによって開発され、効率的で信頼性が高く、高速であり、クロスブラウザのウェブ自動化タスクを可能にします。
Collecting Amazon Product Information with Playwright
Playwrightを使用すると、Amazon(www.amazon.com)にアクセスして製品情報やレビューをクロールするなど、ユーザーの行動をシミュレートできます。CSSセレクターまたはXPathを使用することで、ウェブページの要素を正確に特定し、そのテキストや属性を抽出できます。
Example: Crawling the Amazon Best Sellers List
Playwrightを使用して、Amazonの国際的なベストセラーリストを収集します。手順は次のとおりです。
- ターゲットページにアクセスします。例:https://www.amazon.com/b/?ie=UTF8&node=16857165011&ref_=sv_b_3
- すべての書籍要素(クラス名が
a-section
とa-spacing-base
)を選択します - 書籍要素を反復処理し、タイトル、価格、評価、レビュー数などの情報を抽出します
Deploying a Playwright Example on Leapcell
Playwright Deployment Example on Leapcell
このガイドでは、LeapcellにPlaywrightテストをデプロイするための効率的なアプローチを提供します。ステップバイステップのチュートリアルについては、上記のリンクをたどってください。
Node.js Implementation Code
以下は、Node.jsとPlaywrightを使用したデータ収集の実装です。
const { chromium } = require('playwright'); (async () => { // Launch the browser const browser = await chromium.launch({ headless: true }); const context = await browser.newContext(); const page = await context.newPage(); // Visit the Amazon search page await page.goto('https://www.amazon.com/'); // Search for the keyword "laptop" await page.fill('#twotabsearchtextbox', 'laptop'); await page.click('#nav-search-submit-button'); // Wait for the page to finish loading await page.waitForLoadState('networkidle'); // Get the list of product links const links = await page.evaluate(() => { return Array.from(document.querySelectorAll('.s-result-item h2 a')) .map(a => a.href); }); // Collect product details data const results = []; for (const link of links) { const productPage = await context.newPage(); await productPage.goto(link, { waitUntil: 'networkidle' }); const title = await productPage.textContent('#productTitle'); const rating = await productPage.textContent('#averageCustomerReviews .a-icon-alt').catch(() => 'N/A'); const reviewCount = await productPage.textContent('#acrCustomerReviewText').catch(() => 'N/A'); results.push({ title: title.trim(), rating, reviewCount }); await productPage.close(); } // Output the collected data console.log(results); // Close the browser await browser.close(); })();
Code Analysis
- Initializing Playwright:
chromium.launch({ headless: true })
を使用してブラウザを起動します。 - Navigating to the Amazon Search Page:
page.goto()
を使用してウェブサイトにアクセスし、検索ボックスに入力して検索を送信します。 - Extracting Product Links:
document.querySelectorAll()
を使用して、すべての製品のURLを取得します。 - Collecting Product Details:
- 各製品のページを開きます。
- 製品タイトル(
#productTitle
)を取得します。 - 評価(
#averageCustomerReviews .a-icon-alt
)を取得します。 - レビュー数(
#acrCustomerReviewText
)を取得します。
- Outputting Data and Closing the Browser
Code Optimization
- Error Handling: 一部の製品には評価やレビュー数がない場合があります。
.catch(() => 'N/A')
を使用して、コードがクラッシュするのを防ぎます。 - Automation Efficiency:
await context.newPage()
を使用してコンテキストを再利用し、ページロード速度を向上させます。 - Avoiding Being Blocked:
- プロキシアクセス(Playwrightの
proxy
オプションなど)を使用できます。 userAgent
を調整して、より実際のユーザーのようにすることができます。
- プロキシアクセス(Playwrightの
PlaywrightとNode.jsを使用すると、Amazonのウェブページのデータ収集を効率的に自動化でき、eコマースのデータ分析や競合調査などのシナリオに適しています。
Leapcell: The Next - Gen Serverless Platform for Web Hosting, Async Tasks, and Redis
最後に、Playwrightのデプロイに最適なプラットフォームLeapcellをお勧めします。
1. Multi - Language Support
- Develop with JavaScript, Python, Go, or Rust.
2. Deploy unlimited projects for free
- pay only for usage — no requests, no charges.
3. Unbeatable Cost Efficiency
- Pay - as - you - go with no idle charges.
- Example: $25 supports 6.94M requests at a 60ms average response time.
4. Streamlined Developer Experience
- Intuitive UI for effortless setup.
- Fully automated CI/CD pipelines and GitOps integration.
- Real - time metrics and logging for actionable insights.
5. Effortless Scalability and High Performance
- Auto - scaling to handle high concurrency with ease.
- Zero operational overhead — just focus on building.
Explore more in the documentation!
Leapcell Twitter: https://x.com/LeapcellHQ