from urllib.request import urlopen
import json
import pandas as pd
from bs4 import BeautifulSoup
# you have to obtain your own API key. This API key will not work
apiKey="AIzaSyDBvyE5pYoW3u6JzvP_VOKlUPTLCQfMpc0"
origin="Disneyland"
dest="Universal Studios Hollywood"
apiURL="https://maps.googleapis.com/maps/api/directions/json?origin="+origin.replace(' ', '+')+"&destination="+dest.replace(' ', '+')+"&key="+apiKey
response = urlopen(apiURL)
result = json.load(response)
print(result)
leg = result['routes'][0]['legs'][0]
steps = leg['steps']
print(leg)
print(steps[2])
d = []
d.append(
{
'instructions': leg['start_address'],
'distance': '',
'duration': ''
}
)
for p in range(0,len(steps)-1):
d.append(
{
'instructions': BeautifulSoup(steps[p]['html_instructions'], "html").text,
'distance': steps[p]['distance']['text'],
'duration': steps[p]['duration']['text']
}
)
d.append(
{
'instructions': leg['end_address'],
'distance': '',
'duration': ''
}
)
pd.DataFrame(d)