LoginSignup
0
2

More than 3 years have passed since last update.

python googlemap api でレビューを取得

Last updated at Posted at 2020-10-06

概要

最終目標はスクレイピングした店舗を口コミ等を参考にしてランキング付けするwebサイトを作ることです

前回(python googlemap api を利用したデータ取得)の続きです。
apiを利用して店舗情報の取得まではいけたのですが、'reviews'のパラメータが存在せず、口コミの取得に四苦八苦しました。

参考サイト

Google Map APIからレビューを取得する
PythonでGoogle APIとぐるなび APIのデータを取得してみた

やったこと

Google Map APIからレビューを取得するを見てみると、どうやら'place_id'と'api_key'で'review'の存在するデータにアクセスできるようだ

以下のようにするとレビューを取り出すことが出来た。
astモジュールを用いているのは、bs4で取得したXML形式のデータを辞書型に変換して、レビューのみ取り出したかったからである。

最大で5件までしか取得できないみたいなので、時間等の他の要素でソートして取り出す方法を模索していく


key = 'AIzaSyDdKdbQVGfN2SgQ2BNEkwAPhK1enpJzk_c' # 上記で作成したAPIキーを入れる
placeId = 'ChIJJ4-os2znAGAReJ4AQRGTrcs'

urlName = "https://maps.googleapis.com/maps/api/place/details/json?placeid={0}&key={1}".format(placeId,key)
dataHTML = requests.get(urlName)
soup = BeautifulSoup(dataHTML.content, "html.parser")

soup = ast.literal_eval(str(soup))
pprint.pprint(soup['result']['reviews'])

実行結果

 {'author_name': 'Susie Mead',
  'author_url': 'https://www.google.com/maps/contrib/109736572258034599657/reviews',
  'language': 'en',
  'profile_photo_url': 'https://lh5.googleusercontent.com/-yaP8l2DOlaE/AAAAAAAAAAI/AAAAAAAAAAA/AMZuucnHsZcunKg758to4D5rIfeVjwMqZg/s128-c0x00000000-cc-rp-mo/photo.jpg',
  'rating': 2,
  'relative_time_description': 'a year ago',
  'text': 'The hotel is located in a really interesting area filled with shops '
          'and markets. The room was nice and updated. My downside is the '
          'building was hard to find and no English signs at all and the staff '
          'barley speak English making it very difficult to communicate.',
  'time': 1562469388},
 {'author_name': 'whenuaboynton',
  'author_url': 'https://www.google.com/maps/contrib/118063609090864700050/reviews',
  'language': 'en',
  'profile_photo_url': 'https://lh3.googleusercontent.com/a-/AOh14GjXy-z98y7u9l702EwHvz5DN6AqQWihDh3Fsp-V=s128-c0x00000000-cc-rp-mo',
  'rating': 5,
  'relative_time_description': 'a year ago',
  'text': 'Double room was amazing, biggest bed I’ve ever seen or slept on. '
          'Room far bigger than others experienced in Japan. Couldn’t get a '
          'better location in Osaka.',
  'time': 1569917795}]
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2