最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to use the Google Maps Places API (New) in python? - Stack Overflow

programmeradmin0浏览0评论

I want to use the textSearch (=%7B%22fields%22%3A%22*%22%2C%22resource%22%3A%7B%22textQuery%22%3A%22Spicy%20Vegetarian%20Food%20in%20Sydney%2C%20Australia%22%7D%7D) in Places API (New) to obtain all of the POIs of one specific category in an area, but I'm always getting no return from the code.

My code:

import requests

def get_places_by_category(query, api_key):
    # Base URL for the Text Search API
    base_url = ":searchText"
    
    # Parameters for the request
    params = {
        "query": query,
        "key": api_key
    }

    # Send the request
    response = requests.get(base_url, params=params)

    # Check if the request was successful
    if response.status_code == 200:
        return response.json()
    else:
        return None

# Example usage
api_key = API_KEY  # Replace with the actual API key
query = "restaurant"  # Category of POIs you want to search for

places = get_places_by_category(query, api_key)

if places:
    for place in places['results']:
        print(f"Name: {place['name']}, Address: {place['formatted_address']}")
else:
    print("Failed to get a response from Google Places API (New)")

It keeps returning "Failed to get a response from Google Places API (New)".

发布评论

评论列表(0)

  1. 暂无评论