📌  相关文章
📜  Python|使用 Google Places API 根据搜索查询获取一组地点

📅  最后修改于: 2022-05-13 01:55:37.171000             🧑  作者: Mango

Python|使用 Google Places API 根据搜索查询获取一组地点

Google Places API Web Service允许用户查询各种类别的地点信息,例如场所、著名景点、地理位置等。可以通过邻近度或文本字符串搜索地点。地点搜索会返回地点列表以及每个地点的摘要信息;更多信息可通过地点详情查询获得。

这里使用的是 Google Places Api 的 Text Search Service,它是一个基于字符串返回一组地点信息的网络服务。例如“德里的酒店”或“奥沙瓦附近的鞋店”。该服务以与文本字符串匹配的地点列表和已设置的任何位置偏差作为响应。

要使用此服务,用户必须需要一个 API 密钥,可以从此处获取。

让我们尝试使用Python来使用这个 API 服务。将使用的模块是requestsjson

下面是实现:

# Python program to get a set of 
# places according to your search 
# query using Google Places API
  
# importing required modules
import requests, json
  
# enter your api key here
api_key = 'Your_API_key'
  
# url variable store url
url = "https://maps.googleapis.com/maps/api/place/textsearch/json?"
  
# The text string on which to search
query = input('Search query: ')
  
# get method of requests module
# return response object
r = requests.get(url + 'query=' + query +
                        '&key=' + api_key)
  
# json method of response object convert
#  json format data into python format data
x = r.json()
  
# now x contains list of nested dictionaries
# we know dictionary contain key value pair
# store the value of result key in variable y
y = x['results']
  
# keep looping upto length of y
for i in range(len(y)):
      
    # Print value corresponding to the
    # 'name' key at the ith index of y
    print(y[i]['name'])

输出 :

Search query: Hotels in delhi

ITC Maurya
Le Meridien New Delhi
The Imperial New Delhi
Radisson Blu Hotel New Delhi Paschim Vihar
The Lalit New Delhi
Crowne Plaza New Delhi Rohini
Shangri-La's Eros Hotel, New Delhi
Pride Plaza Hotel Aerocity, New Delhi
The Claridges
The Leela Ambience Convention Hotel, Delhi
Radisson Blu Plaza Delhi Airport
Hotel City Park
Radisson Blu New Delhi Dwarka
The Ashok Hotel
Novotel New Delhi Aerocity
Mapple Emerald
The Metropolitan Hotel and Spa
The Umrao
Pullman New Delhi Aerocity
Welcome Hotel Dwarka