PuppeteerとBrightDataのスクレイピングブラウザを使って、高度なウェブスクレイピング技術を学びます。Amazonなどのサイトからeコマースデータを収集し、ChatGPTでそのデータを分析します。
公開日:2023年4月25日
※動画を再生してから、インタビューを読むのがオススメです。
インターネットには有用なデータがたくさんありますが、残念ながらそのデータは複雑なHTMLの山の奥深くに埋もれていることが多いのです。
データマイニングという言葉は、文字通り、探している貴重な生データを抽出するために、役に立たない汚いマークアップの束を掘り起こさなければならないので、完璧な比喩と言えるでしょう。
インターネットでお金を稼ぐ最も一般的な方法の1つはEコマースとドロップシッピングですが、競争が激しく、何をいつ売ればいいのかを知っておく必要があります。
心配しないでください、私はドロップシッピングのマスタークラスであなたを騙すつもりはありません。
その代わり、Puppeteerというヘッドレスブラウザを使ったウェブスクレイピングについて教えるつもりです。
Puppeteerを使えば、AmazonのようにAPIを提供していないウェブサイトであっても、ほぼすべての一般向けウェブサイトからデータを抽出し、貴重なデータにアクセスすることができます。
やることは、AmazonやeBayのようなウェブサイトでトレンドの商品を見つけ、データセットを構築し、GPT-4のようなAIツールを持ち込んでデータを分析することです。
レビューを書いたり、広告を書いたり、その他必要な作業をほぼすべて自動化します。
さらに、ChatGPTを使って、歴史的に非常に煩わしいコードであるウェブスクレイピングのコードをずっと速く書くためのトリックもお教えします。
しかしその前に、大きな問題があります。
Amazonのような大きなECサイトはボットのような大きなトラフィックを好まず、人間ではないと疑えばIPアドレスをブロックしたり、キャプチャを解かせたりします。
しかし、それは生物以外の生命に対する差別のようなものです。
幸いなことに、今日のビデオのスポンサーであるBrightdataは、Scraping Browserという特別なツールを提供しています。
これはプロキシネットワーク上で動作し、キャプチャ解決、指紋、再試行などのさまざまな組み込み機能を提供し、産業規模でウェブをスクレイピングすることができます。
とはいえ、ウェブからデータを抽出することを真剣に考えるなら、自動IPアドレスローテーションを行うツールが必要になる可能性が非常に高く、このコードを使って無料でBrightdataを試すことができます。
アカウントにサインアップすると、Web Scraper IDEと呼ばれる製品に気づくでしょう。
このビデオでは使いませんが、もしあなたが本気でウェブスクレイピングに取り組んでいるのであれば、テンプレートや追加ツールがたくさん用意されているので、それを活用するのがいいでしょう。
私自身は開発者として、ワークフローを完全にコントロールしたいのです。
そこで、Puppeteerと呼ばれるGoogleのオープンソースツールを使うことにしています。
これはヘッドレスブラウザで、エンドユーザーのようにウェブサイトを表示し、JavaScriptを実行したり、ボタンをクリックしたり、ユーザーができることすべてを行うことで、プログラム的にウェブサイトと対話することができる。
これはかなりクールだが、同じウェブサイトで何度も使っていると、いずれIPフラグを立てられ、使用禁止にされる。
そうなれば、お母さんはWalmart.comで食料品を注文できなくなったと怒ることになります。
そこで登場するのがスクレイピング・ブラウザです。
プロキシネットワークを利用して、このような問題を回避するリモートブラウザなのです。
まず、npmで新しいNode.jsプロジェクトを作成し、Puppeteerをインストールします。
まあ、実際にはPuppeteer Coreをインストールするのだが、これはブラウザそのものを含まない自動化ライブラリであり、今回もリモートブラウザに接続するからだ。
次に、index.jsファイルを作成し、Puppeteerをインポートします。
そこから、runという非同期関数を作成し、ブラウザ自体の変数を宣言します。
このtry catchブロックの中で、ブラウザへの接続を試行します。
エラーが発生した場合は、コンソールログに記録するようにします。
そして最後に、すべてのスクレイピングが終了したら、ブラウザを自動的に閉じます。
意図せずブラウザを開いたままにしておくのは避けたいものです。
さて、tryの中で、ブラウザのWebSocketエンドポイントを使用するPuppeteer接続を待ちます。
この時点で、ブライトデータダッシュボードのプロキシセクションに移動し、新しいスクレイピングブラウザインスタンスを作成することができます。
作成したら、アクセスパラメータに移動し、コードにホストユーザー名とパスワードが戻っていることに気づくでしょう。
これらの値を使用して、WebSocket URLを作成することができます。
ユーザー名とパスワードはコロンで区切り、その後にホストのURLを記述することになります。
これでブラウザに接続できたので、Puppeteerを使って、人間がプログラムでできることはほとんど何でもできるようになりました。
新しいページを作成し、デフォルトのナビゲーションタイムアウトを2分に設定してみましょう。
そこから、インターネット上のあらゆるURLにアクセスすることができます。
Puppeteerには、ドル記号のようにウェブページを解析するのに役立つさまざまなAPIメソッドがあり、これはjQueryがブラウザのドキュメントクエリーセレクタに相当するように感じられます。
これを使うと、DOM内の任意の要素を取得し、そこからテキストコンテンツを抽出することができます。
また、別の方法として、ブラウザのAPIに直接アクセスできるコールバック関数を受け取るpage evaluateを使用することもできます。
このように、ブラウザのコンソールで行うように、document要素を取得して、その外側のHTMLを取得することができます。
それでは、ドキュメントの外側のHTMLをコンソールログに記録してみましょう。
そして、このスクレイパーをテストして、すべてが期待通りに動作していることを確認する準備が整いました。
ターミナルを開き、ファイルに対してnodeコマンドを実行すると、結果としてそのページのHTMLが返ってくるはずです。
これで、産業規模のウェブスクレイピングを行う準備が整いました。
では、Amazonのベストセラー・ページにアクセスするためのコードを更新してみます。
最初の目標は、管理しやすいHTMLの塊を手に入れることです。
ChromeのブラウザDevToolsを開いて、スクレイピングしたい商品のリストをハイライトするまで、HTMLを直接検査します。
理想的には、これらの商品とその価格をすべてJSONオブジェクトとして取得したいです。
すべての商品が、カルーセルのクラスを持つdivに包まれていることに気づくでしょう。
このセレクタを出発点として使用することができます。
Chrome DevToolsにはコピーセレクタの機能もあり、これはかなりクールですが、通常は少しやりすぎです。
コードに戻って、そのセレクタが表示されるまでページが待機するようにします。
そして、ドル記号のクエリーセレクタを使ってDOMからそれを取得し、最後にそれを評価して内部のHTMLを取得することができます。
それでは、コンソールログに記録して、もう一度スクリプトを実行してみましょう。
この時点で、より扱いやすいHTMLの塊ができたので、自分で解析することができます。
しかし、この仕事を終わらせるには、ChatGPTのようなツールを使うのが手っ取り早い方法です。
このHTMLをコピーしてチャットに貼り付け、商品のタイトルと価格を取得し、JSONオブジェクトとして返す人形劇コードを書いてもらうだけでいいのです。
文字通り、最初のトライで、適切なクエリセレクタで要素を取得し、JSONオブジェクトとして要求したデータをフォーマットする、完璧な評価コードを書いてくれます。
そのコードをプロジェクトにコピー&ペーストして、もう一度ノード・スクリプトを実行してみましょう。
これで、ビジネスがスタートしたことになります。
Amazonのトレンド商品に関する独自のカスタムAPIを構築したところですが、eBayやWalmartなど、他のeコマースストアにも同じ手法を適用することができます。
これはかなりクールなことです。
さらに多くのデータを抽出したい場合は、これらの商品のリンクを取得し、スクレイピング・ブラウザを使ってそこに移動し、さらに多くのデータを抽出することも可能です。
各製品をループして、先ほどと同じように、そのURLに移動する方法を使用します。
ただし、この作業を行う際には、サーバーへのリクエストを過剰に送信しないよう、ページ間に少なくとも2秒程度の遅延を設けることをお勧めします。
さて、このように素晴らしいデータを手に入れたことで、可能性は無限に広がっています。
例えば、GPT-4を使って、商品ごとに異なる層をターゲットにした広告を書くことができます。
あるいは、何百万もの商品をベクターデータベースに保存して、AutoGPTツールのようなカスタムAIエージェントを構築し、このデータを使ってAmazonドロップシッピングのビジネスプランを構築することもできるかもしれません。
要するに、AIでクールなことをしたいのであれば、データが必要だということです。
しかし、多くの場合、必要なデータを入手する唯一の方法はウェブスクレイピングであり、安全で効果的な方法でそれを行う方法を知っていることになります。
ご視聴ありがとうございました!次回でお会いしましょう。