0

Here's a couple lines of a larger program

results = requests.get("https://roads.googleapis.com/v1/snapToRoads?path=12.919082641601562,77.65169525146484|12.918915748596191,77.6517105102539|12.918656349182129,77.65177154541016|12.918524742126465,77.6517562866211|12.918295860290527,77.65178680419922|12.918216705322266,77.65177154541016|12.918027877807617,77.65178680419922||12.91647720336914,77.65180206298828|12.91647720336914,77.65180206298828|12.916269302368164,77.65177154541016|12.916149139404297,77.65178680419922|12.916014671325684,77.65177917480469|12.91580867767334,77.65179443359375|12.915785789489746,77.65182495117188|12.913755416870117,77.65186309814453|12.913578987121582,77.65186309814453|12.913309097290039,77.65184783935547|12.913025856018066,77.65186309814453|12.912832260131836,77.65187072753906|12.912651062011719,77.65190124511719|12.912428855895996,77.65188598632812|12.912148475646973,77.65184020996094|12.9120512008667,77.65180206298828|12.91196060180664,77.65179443359375|12.911927223205566,77.65165710449219|12.911907196044922,77.6515884399414|12.911901473999023,77.65150451660156|12.91188907623291,77.65132904052734|12.911890029907227,77.65113067626953|12.911898612976074,77.65096282958984|12.911872863769531,77.6508560180664||12.912422180175781,77.64830&key=AIzaSyAmplaUG26XJGwPrLbky2bHQ-eBmQvZUVU")

print(results.json())

Output:

{'snappedPoints': 
    [
       {'location': {'latitude': 12.919082345679861, 'longitude': 77.65168471404562}, 'originalIndex': 0, 'placeId': 'ChIJ6yP7JIIUrjsRpHSPhEcWRHc'}, {'location': {'latitude': 12.918915069015311, 'longitude': 77.65169005380653}, 'originalIndex': 1, 'placeId': 'ChIJ6yP7JIIUrjsRpHSPhEcWRHc'}, {'location': {'latitude': 12.91865394531316, 'longitude': 77.65169918497911}, 'originalIndex': 2, 'placeId': 'ChIJ6yP7JIIUrjsRpHSPhEcWRHc'}, {'location': {'latitude': 12.91852299728483, 'longitude': 77.65170376406233}, 'originalIndex': 3, 'placeId': 'ChIJ6yP7JIIUrjsRpHSPhEcWRHc'}, {'location': {'latitude': 12.918293368390502, 'longitude': 77.65171179387626}, 'originalIndex': 4, 'placeId': 'ChIJ6yP7JIIUrjsRpHSPhEcWRHc'}, {'location': {'latitude': 12.918214811587053, 'longitude': 77.65171454089874}, 'originalIndex': 5, 'placeId': 'ChIJ6yP7JIIUrjsRpHSPhEcWRHc'}, {'location': {'latitude': 12.91802528954814, 'longitude': 77.65172119976897}, 'originalIndex': 6, 'placeId': 'ChIJY0KcOIIUrjsRkOk78pj9o5I'}, {'location': {'latitude': 12.917911987935893, 'longitude': 77.65172590496891}, 'originalIndex': 7, 'placeId': 'ChIJY0KcOIIUrjsRkOk78pj9o5I'}, {'location': {'latitude': 12.917772027120648, 'longitude': 77.65173171726882}, 'originalIndex': 8, 'placeId': 'ChIJY0KcOIIUrjsRkOk78pj9o5I'}, {'location': {'latitude': 12.917657472888019, 'longitude': 77.65173647447801}, 'originalIndex': 9, 'placeId': 'ChIJY0KcOIIUrjsRkOk78pj9o5I'}, {'location': {'latitude': 12.917551187179303, 'longitude': 77.65174088830797}, 'originalIndex': 10, 'placeId': ...
    ]
}

Now I need to specifically request the 'latitude' and 'longitude' parts of the webpage, and store them in a set. Any idea on how do I call these individual elements?

TDG
  • 5,909
  • 3
  • 30
  • 51

2 Answers2

1

Unfortunately, the short answer is, you can not do this simply from the get request. This answer here explains it very nicely.

This means that you would have to get the entire json table, and obtain the latitude and longitude values locally.

snappoints = results.json()['snappedPoints']
myset = set()
for point in snappoints:
    # this is each individual element in snapPoints array
    myset.add((point['location']['latitude'], point['location']['longitude']))
aoiee
  • 345
  • 3
  • 15
0

I assume you mean the latitude and longitude for each location in the snappedPoints array.

You could use the built-in JSON decoder provided by the Requests module: http://docs.python-requests.org/en/master/user/quickstart/#json-response-content

Or you could use Python's JSON module if you prefer: 2.7 Docs | 3.6 docs

Once you've decoded the JSON response you can just iterate over your locations and grab the parts you want.