目次を見る

すでにご存知の方も多いかと思いますが、この Official Google Places API は、1つの場所につき5つのレビューに制限されています。そのため、開発者は、Google Maps上のあらゆるビジネスからすべてのレビューを取得する機能を持つスクレイピングを検討しています。

Googleのすべての保護と動的にレンダリングされたページをスクレイピングすることは、困難な作業かもしれません。幸いなことに、pythonや他のプログラミング言語でレビューをスクレイピングするために使用できる多くのツールがあります。このブログの記事では、Googleのレビューをスクレイピングするための2つの最も一般的なツールを参照してください:ブラウザエミュレーションとOutscraperプラットフォーム。それぞれは、地図から任意のリストからすべてのレビューを取得するのに十分です。

動的コンテンツをレンダリングするためにブラウザを使用してPythonでGoogleレビューをスクレイピングする

を使用します。 セレン で、Chromeブラウザを制御します。ブラウザはGoogleレビューの動的なページをレンダリングすることになります。Seleniumを使ったレビュースクレーパーの構築を始めるには、以下のものが必要です:

  1. Python 3+。
  2. Chromeブラウザがインストールされています。
  3. Selenium 3.141.0+ (pythonパッケージ)を使用しています。
  4. Chrome Driver(お使いのOS用)。
  5. Parselや、Beautiful Soupのような、HTMLからデータを抽出するライブラリ。

Seleniumとその他のパッケージのインストール

以下のコマンドを実行して、SeleniumとParselのパッケージをインストールします。Parselは、後でHTMLからコンテンツを解析する際に使用します。

				
					pip install selenium
pip install parsel # to extract data from HTML using XPath or CSS selectors
				
			

ブラウザを起動する

ドライバを起動する前に、前のステップを実行し、chromedriverファイルのパスがあることを確認します。次のコードでドライバを初期化します。新しいブラウザウィンドウが開くのが見えるはずです。

				
					from selenium import webdriver


chromedrive_path = './chromedriver' # use the path to the driver you downloaded from previous steps
driver = webdriver.Chrome(chromedrive_path)
				
			

macでは以下のように表示されることがあります: "開発者が確認できないため、chromedriverを開くことができません。" これを克服するには、Finderでcontrolキーを押しながらクロームドライバをクリックし、メニューから「開く」を選択し、表示されるダイアログで「開く」をクリックしてください。開いたターミナルウィンドウに「ChromeDriver was started successfully」と表示されるはずです。それを閉じれば、この後、コードからChromeDriverを起動することができるようになります。

ダウンロード 全てのレビューページ

ドライバを起動すると、いくつかのページを開くことができるようになります。任意のページを開くには、「get」コマンドを使用します。

				
					url = 'https://www.google.com/maps/place/Central+Park+Zoo/@40.7712318,-73.9674707,15z/data=!3m1!5s0x89c259a1e735d943:0xb63f84c661f84258!4m16!1m8!3m7!1s0x89c258faf553cfad:0x8e9cfc7444d8f876!2sTrump+Tower!8m2!3d40.7624284!4d-73.973794!9m1!1b1!3m6!1s0x89c258f1fcd66869:0x65d72e84d91a3f14!8m2!3d40.767778!4d-73.9718335!9m1!1b1?hl=en&hl=en'
driver.get(url)
				
			

Parseのレビュー

ページが開かれると、あなたのコードによって制御されたページがChromeウィンドウに表示されます。以下のコードを実行すると、ドライバーからHTMLページの内容を取得することができます。

				
					page_content = driver.page_source
				
			

HTMLの内容を快適に確認するには、Chromeでブラウザウィンドウの右上にあるChromeメニューを開き、「その他のツール」>「デベロッパーツール」を選択して、デベロッパーコンソールを開いてください。これで、ページの要素を見ることができるようになるはずです。

パイソンでGoogleレビューをスクレイピング
Developer Consoleで取得したいレビューへのXpathを探す

お好みの解析ツールを使って、HTMLページからコンテンツを解析することができます。ここでは パーセル このチュートリアルでは

				
					from parsel import Selector

response = Selector(page_content)
				
			

レビューを繰り返します。

				
					results = []

for el in response.xpath('//div/div[@data-review-id]/div[contains(@class, "content")]'):
    results.append({
        'title': el.xpath('.//div[contains(@class, "title")]/span/text()').extract_first(''),
        'rating': el.xpath('.//span[contains(@aria-label, "stars")]/@aria-label').extract_first('').replace('stars' ,'').strip(),
        'body': el.xpath('.//span[contains(@class, "text")]/text()').extract_first(''),
    })
    
print(results)
				
			

Google Reviews Crawlerからの出力(短縮版)。

				
					[
  {
    'title': 'Wanda Garrett',
    'rating': '5',
    'body': 'Beautiful ✨ park with a family-friendly atmosphere! I had a great time here; seeing all of the animals and learning all of the interesting facts was a fantastic way to spend the day. The zoo is beautifully landscaped and surrounded by …'
  },
  {
    'title': 'Bernadette Bennett',
    'rating': '4',
    'body': 'Worth going for the seals! They are the main attraction and located in the center of the zoo. We watched a live feeding and it was great. The kids loved it. The zoo is well manicured surrounded by gorgeous gardens. Lots of benches to rest …'
  },
  {
    'title': 'Mary Cutrufelli',
    'rating': '3',
    'body': "So not gonna lie... We came from PA. My kid expected to see lions and hippos and zebra from Madagascar. None of that which is there. It's clean it's a nice zoo. I wouldn't go again though."
  },
  ...
]
				
			

ブラウザの停止

それに合わせて、削る前と削った後にドライバーを起動・停止することが重要です。ネットサーフィンの前後でブラウザを開いたり閉じたりするのと同じです。以下のコードを実行して、ブラウザを停止します。

				
					driver.quit()
				
			

Google ReviewsのトリッキーなHTML構造にもかかわらず、SeleniumとXPathとCSSセレクタの十分な知識があれば、スクレイピングでかなり良い結果を出すことができます。ブラウザエミュレータを使用するこの方法は、ブロックされることからあなたを守るはずです。しかし、アプリケーションをスケールアップする場合は、予期せぬ問題を避けるためにプロキシを使用することを検討してください。

マルチプロセッシングとその他の推奨事項

マルチプロセッシング(マルチスレッドではない)でドライバーを動かすことは可能ですが、ドライバー1つにつき1CPUを消費します。十分な数を確保するようにしてください。

PythonでGoogleレビューをスクレイピングする最も簡単な方法

ブラウザを使ったGoogleのデータ抽出には、長所と短所があります。スクレイパーは自前で開発できるが、スケーリング時にブラウザのエミュレーションを処理するために膨大なCPUを持つサーバーを使用するため、大きな出費につながる可能性がある。また、クローラーのメンテナンスやGoogleのサイト変更に伴うアップデートを行う担当者が必要である。

を使うことで OutscraperプラットフォームAPIまたは SDK, Outscraperは、企業や個人がプロキシやブラウザのエミュレーション、開発投資をすることなく、Googleからのレビューのスクレイピングを始めるための最も簡単なソリューションを提供します。

SDKを利用したPythonによるレビューのスクレイピング

1.python3+とこれが必要になります。 パイソンパッケージ.コマンドを実行して、パッケージをインストールします。

				
					pip install google-services-api
				
			

2.からAPIキーを取得します。 プロフィールページ.

3.パッケージをインポートし、キーで初期化します。

4.リンク、プレイスID、または名前を指定して、場所を指定します。

				
					from outscraper import ApiClient


api_cliet = ApiClient(api_key='KEY_FROM_OUTSCRAPER')
response = api_cliet.google_maps_reviews(
    'https://www.google.com/maps/place/Do+or+Dive+Bar/@40.6867831,-73.9570104,17z/data=!3m2!4b1!5s0x89c25b96a0b10eb9:0xfe4f81ff249e280d!4m5!3m4!1s0x89c25b96a0b30001:0x643d0464b3138078!8m2!3d40.6867791!4d-73.9548217',
    language='en',
    limit=100
)
				
			

5.レビューが取得されるまで数秒待ちます。

				
					[
    {
        "name": "Do or Dive Bar",
        "full_address": "1108 Bedford Ave, Brooklyn, NY 11216, United States",
        "borough": "Bedford-Stuyvesant",
        "street": "1108 Bedford Ave",
        "city": "Brooklyn",
        "postal_code": "11216",
        "country_code": "US",
        "country": "United States of America",
        "us_state": "New York",
        "state": "New York",
        "plus_code": null,
        "latitude": 40.686779099999995,
        "longitude": -73.9548217,
        "time_zone": "America/New_York",
        "site": "https://www.doordivebedstuy.com/",
        "phone": "+1 917-867-5309",
        "type": "Bar",
        "rating": 4.5,
        "reviews": 425,
        "reviews_data": [
            {
                "google_id": "0x89c25b96a0b30001:0x643d0464b3138078",
                "autor_link": "https://www.google.com/maps/contrib/115539085325450648866?hl=en-US",
                "autor_name": "Sam Grjaznovs",
                "autor_id": "115539085325450648866",
                "autor_image": "https://lh3.googleusercontent.com/a-/AOh14GgxmEH7a10v6Bo8AFb6OkbyxxfIBPXbMYVAxeSIRA=c0x00000000-cc-rp-ba3",
                "review_text": "Cozy shin dig with an assortment of drinks. They have a strong specialty for 10bucks and merch too. They have out side dining as well as back yard area. Ask for Brandon every other Saturday. He\u2019s hella cute!",
                "review_img_url": "https://lh5.googleusercontent.com/p/AF1QipPNs8QvvdkBonV5wuxdoylFjLY3k7L6muepbDq-",
                "owner_answer": null,
                "owner_answer_timestamp": null,
                "owner_answer_timestamp_datetime_utc": null,
                "review_link": "https://www.google.com/maps/reviews/data=!4m5!14m4!1m3!1m2!1s115539085325450648866!2s0x0:0x643d0464b3138078?hl=en-US",
                "review_rating": 5,
                "review_timestamp": 1603781021,
                "review_datetime_utc": "10/27/2020 06:43:41",
                "review_likes": 0,
                "reviews_id": "7222934207919784056"
            },
            {
                "google_id": "0x89c25b96a0b30001:0x643d0464b3138078",
                "autor_link": "https://www.google.com/maps/contrib/110571545135018844510?hl=en-US",
                "autor_name": "Arabella Stephens",
                "autor_id": "110571545135018844510",
                "autor_image": "https://lh3.googleusercontent.com/a-/AOh14GisqDfheDO0Aq0cu1Z7YBTbzLyvSEvM3IMDKg3q=c0x00000000-cc-rp",
                "review_text": "Great atmosphere, always fun vibe and good beers. I live in the area and this is a very reliable standby. Would recommend to anyone who is not pretentious and likes a bit of clutter in their watering hole.",
                "review_img_url": "https://lh3.googleusercontent.com/a-/AOh14GisqDfheDO0Aq0cu1Z7YBTbzLyvSEvM3IMDKg3q",
                "owner_answer": null,
                "owner_answer_timestamp": null,
                "owner_answer_timestamp_datetime_utc": null,
                "review_link": "https://www.google.com/maps/reviews/data=!4m5!14m4!1m3!1m2!1s110571545135018844510!2s0x0:0x643d0464b3138078?hl=en-US",
                "review_rating": 5,
                "review_timestamp": 1614111762,
                "review_datetime_utc": "02/23/2021 20:22:42",
                "review_likes": 0,
                "reviews_id": "7222934207919784056"
            },
            {
                "google_id": "0x89c25b96a0b30001:0x643d0464b3138078",
                "autor_link": "https://www.google.com/maps/contrib/101725757133547547783?hl=en-US",
                "autor_name": "Jack Parker",
                "autor_id": "101725757133547547783",
                "autor_image": "https://lh3.googleusercontent.com/a-/AOh14GjFK9CLb8__u5PtJzH1rGuX4DVgPvjaEeIkSJnCNw=c0x00000000-cc-rp",
                "review_text": "All the bartenders are rad. Cheap drinks, and a nice backyard. They have space heaters, but I would still recommend bundling up if you plan on spending a while there. Jeopardy night is always fun too. Can\u2019t wait to sit inside again!",
                "review_img_url": "https://lh3.googleusercontent.com/a-/AOh14GjFK9CLb8__u5PtJzH1rGuX4DVgPvjaEeIkSJnCNw",
                "owner_answer": null,
                "owner_answer_timestamp": null,
                "owner_answer_timestamp_datetime_utc": null,
                "review_link": "https://www.google.com/maps/reviews/data=!4m5!14m4!1m3!1m2!1s101725757133547547783!2s0x0:0x643d0464b3138078?hl=en-US",
                "review_rating": 5,
                "review_timestamp": 1611947492,
                "review_datetime_utc": "01/29/2021 19:11:32",
                "review_likes": 0,
                "reviews_id": "7222934207919784056"
            },
            ...
        ]
]
				
			

Pythonパッケージ ► https://pypi.org/project/google-services-api
API ► https://app.outscraper.com/api-docs

ビデオチュートリアル

よくある質問

最も頻繁な質問と回答

OutscraperのGoogle Maps Reviews APIにより、全てのGoogle Mapsのレビューをスクレイピングすることが可能です。OutscraperのAPIサービスでは、制限なくスクレイピングすることができます。

Google MapsのレビューのAPIサービスがあります。これがOutscraperのGoogle Maps Reviews APIです。Outscraperのサービスのおかげで、Google Mapsのすべてのレビューをエクスポートしてダウンロードすることができます。

PythonとSeleniumでレビューをスクレイピングすることができます。詳しくは記事 "Pythonで全てのGoogleレビューをスクレイピング“.


ヴラド

プロジェクトマネージャー Linkedin