[PYTHON] Gurunavi API access method

Gurunavi API access method

① Access the following URL and create an account. https://api.gnavi.co.jp/api/

② A key will be issued, so copy it.

③ Access the restaurant API. Example: A sushi restaurant whose address is Ginza

https://api.gnavi.co.jp/RestSearchAPI/v3/?keyid=Key issued in ②&address=%E9%8A%80%E5%BA%A7&category_s=RSFST03001

④ Explanation Restaurant Search API: https://api.gnavi.co.jp/RestSearchAPI/v3/ keyid: The key issued in ② address: address category_s: Restaurant system (sushi restaurant, yakiniku restaurant, etc ...)

⑤ Access with Python

access_gurunabi.py


#Module import
import json
import urllib.request
import ssl

#Specify authentication method as TLSv1
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)

#Specifying the value of the parameter to be passed to the API
# https://api.gnavi.co.jp/api/manual/restsearch/
# https://api.gnavi.co.jp/api/tools/     #You can test the API here
base_url = "https://api.gnavi.co.jp/RestSearchAPI/v3/"

key =Key issued in ②

#####For the above key, specify the keyid obtained when creating the Gurunavi API account.
# shop_name = "Grilled meat" #If you want to include the store name, uncomment it
g_code = 'RSFST03001' #Sushi code
address = 'Ginza'
#Definition of functions that use the API
def gnavi_api(g_code,address):
    params = urllib.parse.urlencode({   
        'keyid': key,
        # 'name' : shop_name, #If you want to include the store name, uncomment it
        'category_s' : g_code,
        'address' : address
    })
    url = base_url + '?' + params
    print(url)
    response = urllib.request.urlopen(url,context=context)
    return response.read()

#Get information from the API using a function
data = gnavi_api(g_code,address)

#Convert the acquired information from JSON format to dictionary type
read_data = json.loads(data)["rest"]

#Create a list to store a list of store names
list_name = []

#Define a function that loops by store and adds the store name to list
def get_name(read_data):
    for dic in read_data:
        list_name.append(dic.get("name")) 
        #list_name.append(dic.get("address")) #Click here if you want to get an address
    return list_name

#Execute the function to get a list of store names
get_name(read_data)

print(list_name)

Recommended Posts

Gurunavi API access method
Access the Twitter API in Python
Google API access token and refresh token
Access the Docker Remote API with Requests