LoginSignup
32
25

More than 5 years have passed since last update.

お父さん必見!週末お出かけ候補リストをかんたん作成(Python、Directions API)

Last updated at Posted at 2018-10-09

本投稿の背景

家族でお出かけする際、
毎回どこに行くかを探すのに迷いますし、行き先を決めるのに時間がかかりますよね。

■自分が行き先を決める時に行うフロー

 1. いこーよ 「https://iko-yo.net/」関東の週間ランキングをみる
 2. 行き先スポットの詳細情報を確認
 3. 2.で確認した行き先スポットを 自宅からgoogleマップで
     距離、所要時間(Googleマップで出発時刻を設定)を調べる

 4. 3.で距離、予想時間が長い場合は、次の候補を探す
 ※1~4 の繰り返しをして、行き先を検索してます

image.png

この行き先スポットが「いい」と思って、自宅からの所要時間を見ると2時間以上時間が掛かったり、
さっきのスポットがよかったと思って見返そうとしたら、ブラウザのタブがたくさんあってどれだっけ?とタブを探したりして、無駄に時間がかかっていました。

その無駄な時間をなくすため、自分でリストを作ればいいと思いました。

本投稿で実現すること

1.お出かけスポットを取得
2.自宅から各スポットの所要時間を取得

⇒1.はPythonのスクレイピング
 2.はGoogle Map Platform の Directions API
 を使用します

作成する 週末お出かけ候補リスト のイメージ

いこーよのWebページをスクレイピングして、その情報から Directions APIを使用して出発地点からの距離、所要時間を算出したリストをPythonで作成します
image.png

事前準備(Google Map Platform で APIキーを申請)

Google Map Platform で Directions APIを使用する場合、KEY を申請する必要があります。

APIを使用する KEY の取得申請方法とGoogle MAP APIの使用方法、Directions APIのパラメータ説明、Pythonスクリプトのサンプルは、以下の投稿でまとめましたのでこちらを参照ください

Directions API(Google Maps Platform) をコールしてルート間の距離と所要時間をPythonで取得する

Pythonスクリプト(Google Map Platform Directions API を使用)

getspots.py
import urllib.request
from bs4 import BeautifulSoup
import urllib.request, json
import urllib.parse
import datetime
import csv

def getspotdata(dep_time,region,origin,csvpath):

    #行き先をURLを格納する配列(リスト)
    arrspot= []

    #いこーよの週間ランキングは1ページ 10件 x 3なので
    #これを3回繰り返す
    for i in range(1, 4):

        #URLを設定
        url = "https://iko-yo.net/rankings/regions/" + str(region) + "/weekly?page=" + str(i)

        #URLリンクを開く
        response = urllib.request.urlopen(url)
        html = response.read()

        # htmlをBeautifulSoupで扱う
        soup = BeautifulSoup(html, "html.parser")

        # a 要素全てをリストに格納
        span = soup.find_all("a")

        #取得件数のカウント件数の初期化
        i = 0

        #a 要素全てを確認して、名称とURLを取得
        for tag in span:

            #a 要素にある classの値を取得
            tagstring = tag.get("class")

            #class がないのはNone で返るのでその対応
            if tagstring is not None:

                #c-link--underlineがあるかの判断
                if tagstring.count("c-link--underline") > 0:

                    target = tag.string

                    #口コミ ○○件 のclassも"c-link--underline" なので除外
                    if target.find("口コミ") == -1 :

                        #取得件数のカウント
                        i += 1

                        #リストに行き先名、情報リンクを取得、格納する
                        #print(tag.string)
                        #print(tag.get("href"))
                        arrspot.append([tag.string, "https://iko-yo.net" + tag.get("href")])                

                        #1ページあたり10件の情報なので、10件取得したら処理を抜ける
                        if i > 9:
                            break


    #Google Maps Platform Direction API から情報取得

    #Google MapsDdirections API endpoint
    endpoint = 'https://maps.googleapis.com/maps/api/directions/json?'
    api_key = '<申請したAPIキーを設定>'

    #google map url 
    googlemap_api = "https://www.google.com/maps/dir/?api=1"

    #出発時間をいれたURL
    googlemap_uncertify = "https://www.google.com/maps/dir/"

    #UNIX時間の算出
    dtime = datetime.datetime.strptime(dep_time, '%Y/%m/%d %H:%M')
    plus_jst = dtime + datetime.timedelta(hours=9) #JSTなので +9時間
    unix_time = int(dtime.timestamp()) #UNIX時間を算出
    plus_jst  = int(plus_jst.timestamp()) #UNIX時間をJSTで算出 ※GoogleMapのURLに設定する


    #現在時間のUNIX時間算出
    now = datetime.datetime.now()
    #now = now + datetime.timedelta(hours=9) #JSTなので +9時間
    now_ts = int(now.timestamp())    

    #指定した時間が現在よりも過去の場合、現在時間を設定
    if now_ts > unix_time:
        unix_time = now_ts   
        dep_time = datetime.datetime.now().strftime('%Y/%m/%d %H:%M')

    #csv出力用リスト
    datalist=[]
    datalist.append(['ランキング','行き先','行き先情報リンク','距離','所要時間','GoogleMapリンク','出発時間(' + str(dep_time) + ')設定 GoogleMapリンク'])


    #1~30位までの行き先の所要時間を Direction API から取得する
    ranking =0

    for s in arrspot:

        #目的地を取得
        destination=s[0].replace(' ','+')

        #Building the URL for the request
        nav_request = 'language=ja&origin={}&destination={}&departure_time={}&key={}'.format(origin,destination,unix_time,api_key)
        nav_request = urllib.parse.quote_plus(nav_request, safe='=&')
        request = endpoint + nav_request

        #Sends the request and reads the response.
        response = urllib.request.urlopen(request).read()   

        #Loads response as JSON
        directions = json.loads(response)

        for key in directions['routes']:
            #print(key) # titleのみ参照
            #print(key['legs']) 
            for key2 in key['legs']:
                print(destination + " : " + key2['duration_in_traffic']['text'])               

                #Google Maps URLs を参考にしたURL
                googlemapurl_p = '&origin={}&destination={}&travelmode=driving'.format(origin,destination)
                googlemap_url = googlemap_api + googlemapurl_p

                #出発時間を設定したURL 
                googlemap_uncertify_p ='{}/{}/data=!4m6!4m5!2m3!6e0!7e2!8j{}!3e0'.format(origin,destination,plus_jst)
                googlemap_url_uncertify = googlemap_uncertify + googlemap_uncertify_p

                #行き先リンク
                #'=HYPERLINK を埋め込む設定
                spoturl='=HYPERLINK("' + s[1] +'","リンク")'
                gglmap ='=HYPERLINK("' + googlemap_url +'","リンク")'
                gglmap_u ='=HYPERLINK("' + googlemap_url_uncertify +'","リンク")'
                ranking += 1

                datalist.append([ranking,s[0], spoturl,key2['distance']['text'],key2['duration_in_traffic']['text'], gglmap,gglmap_u]) 

    #CSV出力
    csvpath = r"'"+ csvfile + "'"
    csvpath = csvpath.replace("'","")

    with open(csvpath, 'w') as file:
        writer = csv.writer(file, lineterminator='\n')
        writer.writerows(datalist)

if __name__ == '__main__':

    #いつ出発するか
    dep_time = input('いつ出発?(過去時間は現在時刻に変換します): yyyy/mm/dd hh:mm 形式 例)2018/10/06 10:00 > ')

    #どの地域か
    region = input('地域を入力:例)  1:北海道:東北、2:北陸:甲信越、3:関東、4:東海、5:関西、6:中国:四国、7:九州:沖縄> ')

    #どこから出発するかを入力
    origin = input('どこから出発?: 例)東京駅、大宮駅 等 > ').replace(' ','+')

    #お出かけスポット一覧のcsv名
    csvfile = input('お出かけスポット一覧出力csv名: 例)d:\list.csv > ')


    getspotdata(dep_time,region,origin,csvfile)

Pythonスクリプトの実行方法は、 Windows環境でPythonスクリプト(.py)の実行方法
を参考ください。

Pythonスクリプト(getspots.py)で作成した「週末お出かけ候補リスト」CSVファイル

※Pythonスクリプトを実行時に入力した条件です。
 ・いつ出発するか:2018/10/13 10:00
 ・どの地域か:3
 ・どこから出発するか:東川口駅
 ・お出かけスポット一覧のcsv名:d:\spotlist.csv

以下、条件で出力された「週末お出かけ候補リスト」CSVファイル

ランキング,行き先,行き先情報リンク,距離,所要時間,GoogleMapリンク,出発時間(2018/10/13 10:00)設定 GoogleMapリンク
1,ツインリンクもてぎ,"=HYPERLINK(""https://iko-yo.net/facilities/1552"",""リンク"")",125 km,1時間49分,"=HYPERLINK(""https://www.google.com/maps/dir/?api=1&origin=東川口駅&destination=ツインリンクもてぎ&travelmode=driving"",""リンク"")","=HYPERLINK(""https://www.google.com/maps/dir/東川口駅/ツインリンクもてぎ/data=!4m6!4m5!2m3!6e0!7e2!8j1539424800!3e0"",""リンク"")"
2,MORI Building DIGITAL ART MUSEUM: EPSON teamLab Borderless,"=HYPERLINK(""https://iko-yo.net/facilities/101198"",""リンク"")",42.7 km,46分,"=HYPERLINK(""https://www.google.com/maps/dir/?api=1&origin=東川口駅&destination=MORI+Building+DIGITAL+ART+MUSEUM:+EPSON+teamLab+Borderless&travelmode=driving"",""リンク"")","=HYPERLINK(""https://www.google.com/maps/dir/東川口駅/MORI+Building+DIGITAL+ART+MUSEUM:+EPSON+teamLab+Borderless/data=!4m6!4m5!2m3!6e0!7e2!8j1539424800!3e0"",""リンク"")"
3,東京あそびマーレ,"=HYPERLINK(""https://iko-yo.net/facilities/104493"",""リンク"")",63.9 km,1時間43分,"=HYPERLINK(""https://www.google.com/maps/dir/?api=1&origin=東川口駅&destination=東京あそびマーレ&travelmode=driving"",""リンク"")","=HYPERLINK(""https://www.google.com/maps/dir/東川口駅/東京あそびマーレ/data=!4m6!4m5!2m3!6e0!7e2!8j1539424800!3e0"",""リンク"")"
4,日本科学未来館,"=HYPERLINK(""https://iko-yo.net/facilities/175"",""リンク"")",43.1 km,47分,"=HYPERLINK(""https://www.google.com/maps/dir/?api=1&origin=東川口駅&destination=日本科学未来館&travelmode=driving"",""リンク"")","=HYPERLINK(""https://www.google.com/maps/dir/東川口駅/日本科学未来館/data=!4m6!4m5!2m3!6e0!7e2!8j1539424800!3e0"",""リンク"")"
5,リトルプラネット ららぽーと新三郷,"=HYPERLINK(""https://iko-yo.net/facilities/111732"",""リンク"")",17.1 km,31分,"=HYPERLINK(""https://www.google.com/maps/dir/?api=1&origin=東川口駅&destination=リトルプラネット+ららぽーと新三郷&travelmode=driving"",""リンク"")","=HYPERLINK(""https://www.google.com/maps/dir/東川口駅/リトルプラネット+ららぽーと新三郷/data=!4m6!4m5!2m3!6e0!7e2!8j1539424800!3e0"",""リンク"")"
6,ファンタジーキッズリゾート海老名,"=HYPERLINK(""https://iko-yo.net/facilities/750"",""リンク"")",79.6 km,1時間55分,"=HYPERLINK(""https://www.google.com/maps/dir/?api=1&origin=東川口駅&destination=ファンタジーキッズリゾート海老名&travelmode=driving"",""リンク"")","=HYPERLINK(""https://www.google.com/maps/dir/東川口駅/ファンタジーキッズリゾート海老名/data=!4m6!4m5!2m3!6e0!7e2!8j1539424800!3e0"",""リンク"")"
7,Studio Make-Over 武蔵浦和 In My Home-ZooAdventure,"=HYPERLINK(""https://iko-yo.net/facilities/106470"",""リンク"")",13.3 km,21分,"=HYPERLINK(""https://www.google.com/maps/dir/?api=1&origin=東川口駅&destination=Studio+Make-Over+武蔵浦和+In+My+Home-ZooAdventure&travelmode=driving"",""リンク"")","=HYPERLINK(""https://www.google.com/maps/dir/東川口駅/Studio+Make-Over+武蔵浦和+In+My+Home-ZooAdventure/data=!4m6!4m5!2m3!6e0!7e2!8j1539424800!3e0"",""リンク"")"
8,クロスポ 千葉浜野店,"=HYPERLINK(""https://iko-yo.net/facilities/66158"",""リンク"")",64.5 km,1時間11分,"=HYPERLINK(""https://www.google.com/maps/dir/?api=1&origin=東川口駅&destination=クロスポ 千葉浜野店&travelmode=driving"",""リンク"")","=HYPERLINK(""https://www.google.com/maps/dir/東川口駅/クロスポ 千葉浜野店/data=!4m6!4m5!2m3!6e0!7e2!8j1539424800!3e0"",""リンク"")"
9,東京ドームシティ  ASOBono!(アソボーノ),"=HYPERLINK(""https://iko-yo.net/facilities/3879"",""リンク"")",29.3 km,33分,"=HYPERLINK(""https://www.google.com/maps/dir/?api=1&origin=東川口駅&destination=東京ドームシティ++ASOBono!(アソボーノ)&travelmode=driving"",""リンク"")","=HYPERLINK(""https://www.google.com/maps/dir/東川口駅/東京ドームシティ++ASOBono!(アソボーノ)/data=!4m6!4m5!2m3!6e0!7e2!8j1539424800!3e0"",""リンク"")"
10,チームラボ 学ぶ!未来の遊園地 ららぽーと富士見店,"=HYPERLINK(""https://iko-yo.net/facilities/24539"",""リンク"")",24.5 km,45分,"=HYPERLINK(""https://www.google.com/maps/dir/?api=1&origin=東川口駅&destination=チームラボ+学ぶ!未来の遊園地+ららぽーと富士見店&travelmode=driving"",""リンク"")","=HYPERLINK(""https://www.google.com/maps/dir/東川口駅/チームラボ+学ぶ!未来の遊園地+ららぽーと富士見店/data=!4m6!4m5!2m3!6e0!7e2!8j1539424800!3e0"",""リンク"")"
11,ギャラクシティ,"=HYPERLINK(""https://iko-yo.net/facilities/17772"",""リンク"")",15.2 km,29分,"=HYPERLINK(""https://www.google.com/maps/dir/?api=1&origin=東川口駅&destination=ギャラクシティ&travelmode=driving"",""リンク"")","=HYPERLINK(""https://www.google.com/maps/dir/東川口駅/ギャラクシティ/data=!4m6!4m5!2m3!6e0!7e2!8j1539424800!3e0"",""リンク"")"
12,国立科学博物館,"=HYPERLINK(""https://iko-yo.net/facilities/893"",""リンク"")",23.2 km,39分,"=HYPERLINK(""https://www.google.com/maps/dir/?api=1&origin=東川口駅&destination=国立科学博物館&travelmode=driving"",""リンク"")","=HYPERLINK(""https://www.google.com/maps/dir/東川口駅/国立科学博物館/data=!4m6!4m5!2m3!6e0!7e2!8j1539424800!3e0"",""リンク"")"
13,ファンタジーキッズリゾート武蔵村山,"=HYPERLINK(""https://iko-yo.net/facilities/61217"",""リンク"")",78.0 km,1時間25分,"=HYPERLINK(""https://www.google.com/maps/dir/?api=1&origin=東川口駅&destination=ファンタジーキッズリゾート武蔵村山&travelmode=driving"",""リンク"")","=HYPERLINK(""https://www.google.com/maps/dir/東川口駅/ファンタジーキッズリゾート武蔵村山/data=!4m6!4m5!2m3!6e0!7e2!8j1539424800!3e0"",""リンク"")"
14,アネビートリムパーク お台場店,"=HYPERLINK(""https://iko-yo.net/facilities/22770"",""リンク"")",42.5 km,45分,"=HYPERLINK(""https://www.google.com/maps/dir/?api=1&origin=東川口駅&destination=アネビートリムパーク お台場店&travelmode=driving"",""リンク"")","=HYPERLINK(""https://www.google.com/maps/dir/東川口駅/アネビートリムパーク お台場店/data=!4m6!4m5!2m3!6e0!7e2!8j1539424800!3e0"",""リンク"")"
15,軽井沢おもちゃ王国,"=HYPERLINK(""https://iko-yo.net/facilities/168"",""リンク"")",186 km,2時間46分,"=HYPERLINK(""https://www.google.com/maps/dir/?api=1&origin=東川口駅&destination=軽井沢おもちゃ王国&travelmode=driving"",""リンク"")","=HYPERLINK(""https://www.google.com/maps/dir/東川口駅/軽井沢おもちゃ王国/data=!4m6!4m5!2m3!6e0!7e2!8j1539424800!3e0"",""リンク"")"
16,森の空中あそび「パカブ」,"=HYPERLINK(""https://iko-yo.net/facilities/89470"",""リンク"")",108 km,2時間23分,"=HYPERLINK(""https://www.google.com/maps/dir/?api=1&origin=東川口駅&destination=森の空中あそび「パカブ」&travelmode=driving"",""リンク"")","=HYPERLINK(""https://www.google.com/maps/dir/東川口駅/森の空中あそび「パカブ」/data=!4m6!4m5!2m3!6e0!7e2!8j1539424800!3e0"",""リンク"")"
17,trampon(トランポン),"=HYPERLINK(""https://iko-yo.net/facilities/93345"",""リンク"")",77.8 km,2時間0分,"=HYPERLINK(""https://www.google.com/maps/dir/?api=1&origin=東川口駅&destination=trampon(トランポン)&travelmode=driving"",""リンク"")","=HYPERLINK(""https://www.google.com/maps/dir/東川口駅/trampon(トランポン)/data=!4m6!4m5!2m3!6e0!7e2!8j1539424800!3e0"",""リンク"")"
18,手裏剣道場 新宿忍者からくり屋敷,"=HYPERLINK(""https://iko-yo.net/facilities/69543"",""リンク"")",30.6 km,45分,"=HYPERLINK(""https://www.google.com/maps/dir/?api=1&origin=東川口駅&destination=手裏剣道場+新宿忍者からくり屋敷&travelmode=driving"",""リンク"")","=HYPERLINK(""https://www.google.com/maps/dir/東川口駅/手裏剣道場+新宿忍者からくり屋敷/data=!4m6!4m5!2m3!6e0!7e2!8j1539424800!3e0"",""リンク"")"
19,レゴランド・ディスカバリー・センター東京,"=HYPERLINK(""https://iko-yo.net/facilities/15693"",""リンク"")",42.2 km,45分,"=HYPERLINK(""https://www.google.com/maps/dir/?api=1&origin=東川口駅&destination=レゴランド・ディスカバリー・センター東京&travelmode=driving"",""リンク"")","=HYPERLINK(""https://www.google.com/maps/dir/東川口駅/レゴランド・ディスカバリー・センター東京/data=!4m6!4m5!2m3!6e0!7e2!8j1539424800!3e0"",""リンク"")"
20,宝石探し トレジャーストーンパーク,"=HYPERLINK(""https://iko-yo.net/facilities/22338"",""リンク"")",158 km,2時間0分,"=HYPERLINK(""https://www.google.com/maps/dir/?api=1&origin=東川口駅&destination=宝石探し トレジャーストーンパーク&travelmode=driving"",""リンク"")","=HYPERLINK(""https://www.google.com/maps/dir/東川口駅/宝石探し トレジャーストーンパーク/data=!4m6!4m5!2m3!6e0!7e2!8j1539424800!3e0"",""リンク"")"
21,キッズキャッスル三郷,"=HYPERLINK(""https://iko-yo.net/facilities/19401"",""リンク"")",14.5 km,23分,"=HYPERLINK(""https://www.google.com/maps/dir/?api=1&origin=東川口駅&destination=キッズキャッスル三郷&travelmode=driving"",""リンク"")","=HYPERLINK(""https://www.google.com/maps/dir/東川口駅/キッズキャッスル三郷/data=!4m6!4m5!2m3!6e0!7e2!8j1539424800!3e0"",""リンク"")"
22,オービィ横浜,"=HYPERLINK(""https://iko-yo.net/facilities/20093"",""リンク"")",65.8 km,1時間15分,"=HYPERLINK(""https://www.google.com/maps/dir/?api=1&origin=東川口駅&destination=オービィ横浜&travelmode=driving"",""リンク"")","=HYPERLINK(""https://www.google.com/maps/dir/東川口駅/オービィ横浜/data=!4m6!4m5!2m3!6e0!7e2!8j1539424800!3e0"",""リンク"")"
23,遊園地 浅草花やしき,"=HYPERLINK(""https://iko-yo.net/facilities/202"",""リンク"")",27.4 km,38分,"=HYPERLINK(""https://www.google.com/maps/dir/?api=1&origin=東川口駅&destination=遊園地+浅草花やしき&travelmode=driving"",""リンク"")","=HYPERLINK(""https://www.google.com/maps/dir/東川口駅/遊園地+浅草花やしき/data=!4m6!4m5!2m3!6e0!7e2!8j1539424800!3e0"",""リンク"")"
24,京王れーるランド,"=HYPERLINK(""https://iko-yo.net/facilities/1654"",""リンク"")",59.5 km,1時間33分,"=HYPERLINK(""https://www.google.com/maps/dir/?api=1&origin=東川口駅&destination=京王れーるランド&travelmode=driving"",""リンク"")","=HYPERLINK(""https://www.google.com/maps/dir/東川口駅/京王れーるランド/data=!4m6!4m5!2m3!6e0!7e2!8j1539424800!3e0"",""リンク"")"
25,キドキドよみうりランド店,"=HYPERLINK(""https://iko-yo.net/facilities/16493"",""リンク"")",51.8 km,1時間19分,"=HYPERLINK(""https://www.google.com/maps/dir/?api=1&origin=東川口駅&destination=キドキドよみうりランド店&travelmode=driving"",""リンク"")","=HYPERLINK(""https://www.google.com/maps/dir/東川口駅/キドキドよみうりランド店/data=!4m6!4m5!2m3!6e0!7e2!8j1539424800!3e0"",""リンク"")"
26,トランポランドsaitama(トランポランド埼玉),"=HYPERLINK(""https://iko-yo.net/facilities/26869"",""リンク"")",24.0 km,44分,"=HYPERLINK(""https://www.google.com/maps/dir/?api=1&origin=東川口駅&destination=トランポランドsaitama(トランポランド埼玉)&travelmode=driving"",""リンク"")","=HYPERLINK(""https://www.google.com/maps/dir/東川口駅/トランポランドsaitama(トランポランド埼玉)/data=!4m6!4m5!2m3!6e0!7e2!8j1539424800!3e0"",""リンク"")"
27,わなびばきっずぱーく333,"=HYPERLINK(""https://iko-yo.net/facilities/103019"",""リンク"")",32.8 km,48分,"=HYPERLINK(""https://www.google.com/maps/dir/?api=1&origin=東川口駅&destination=わなびばきっずぱーく333&travelmode=driving"",""リンク"")","=HYPERLINK(""https://www.google.com/maps/dir/東川口駅/わなびばきっずぱーく333/data=!4m6!4m5!2m3!6e0!7e2!8j1539424800!3e0"",""リンク"")"
28,カンドゥー,"=HYPERLINK(""https://iko-yo.net/facilities/20092"",""リンク"")",43.7 km,49分,"=HYPERLINK(""https://www.google.com/maps/dir/?api=1&origin=東川口駅&destination=カンドゥー&travelmode=driving"",""リンク"")","=HYPERLINK(""https://www.google.com/maps/dir/東川口駅/カンドゥー/data=!4m6!4m5!2m3!6e0!7e2!8j1539424800!3e0"",""リンク"")"
29,多摩六都科学館,"=HYPERLINK(""https://iko-yo.net/facilities/1617"",""リンク"")",30.4 km,58分,"=HYPERLINK(""https://www.google.com/maps/dir/?api=1&origin=東川口駅&destination=多摩六都科学館&travelmode=driving"",""リンク"")","=HYPERLINK(""https://www.google.com/maps/dir/東川口駅/多摩六都科学館/data=!4m6!4m5!2m3!6e0!7e2!8j1539424800!3e0"",""リンク"")"
30,NHKスタジオパーク(エヌエイチケイスタジオパーク),"=HYPERLINK(""https://iko-yo.net/facilities/200"",""リンク"")",32.8 km,46分,"=HYPERLINK(""https://www.google.com/maps/dir/?api=1&origin=東川口駅&destination=NHKスタジオパーク(エヌエイチケイスタジオパーク)&travelmode=driving"",""リンク"")","=HYPERLINK(""https://www.google.com/maps/dir/東川口駅/NHKスタジオパーク(エヌエイチケイスタジオパーク)/data=!4m6!4m5!2m3!6e0!7e2!8j1539424800!3e0"",""リンク"")"

上記、「週末お出かけ候補リスト」CSVをクリックすると
Excelが起動して以下の情報を確認できます。

また、CSV作成時にハイパーリンク(いこーよ基本情報、GoogleMapのリンク)を設定しているので、[リンク]の文字をクリックすると各情報のページが開くように設定しています。

「週末お出かけ候補リスト」csvファイルをクリックしてExcelが起動したサンプル

image.png

列名 説明
ランキング いこーよの1~30位のランキング
行き先 行き先スポット名
行き先情報リンク クリックすると「いこーよ」の基本情報へとびます
距離 Python実行時に入力した出発地からの距離(Directions APIより取得)
所要時間 Python実行時に入力した出発時間 を考慮した 出発地点から目的地までの時間(Directions APIより取得)
GoogleMapリンク 出発地、目的地、車で移動を設定したGoogleマップのURL
出発時間を設定した GoogleMapリンク 出発地、目的地、車で移動、出発時間を設定した GoogleマップのURL

Pythonスクレイピング、Directions APIを組み合わせて、実現したい「週末お出かけ候補リスト」ができました!

Googleマップ URL設定

GoogleマップのURLリンクの作成方法は以下のURLを参考にしました。
https://developers.google.com/maps/documentation/urls/guide#directions-action

上記ドキュメントでは、出発時刻についてのパラメータが記載されていないので、
今回、実施したいことで設定できるのは、origin(出発地)、destination(目的地)、travelmode(車、徒歩、電車)しか設定できません。

また、ドキュメントには
api=1 を設定しないとパラメータが無視されるとあります

>Important: The parameter api=1 identifies the version of Maps URLs this URL is intended for.
>This parameter is required in every request.
>The only valid value is 1. If api=1 is NOT present in the URL, all parameters are >ignored and the default Google Maps app will launch,
>either in a browser or the Google Maps mobile app, depending on the platform in use(for example, https://www.google.com/maps).



Googleマップを開いたとき、出発時刻を設定するのはしんどいので
「出発時間を設定した GoogleMapのURL」は、外部サイトを参考にしてます。
https://mstickles.wordpress.com/2015/06/23/gmaps-urls-diropt3/

※上記、api=1 としてないので、そのうち パラメータが無視される可能性がありますので注意

参考リンク

Directions API(Google Maps Platform) をコールしてルート間の距離と所要時間をPythonで取得する

Windows環境でPythonスクリプト(.py)の実行方法 を参考ください。

Googleマップ URLリンクの作成方法

Googleマップ URLリンクの作成方法 出発時間指定などの方法

32
25
1

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
32
25