I am trying to search restaurants by location and get their menus, address etc. When I opened the site manually, I saw that the data was available via 2 requests. 1 for the location search, second for the menuId. Since, the api url takes Latitude and Longitude as input for search, I added a code for that. However, I don't know what to do with the trackingId part. How to get this, if at all possible for subsequent searches. Also, I am thinking there is no way to know which restaurant has which menuID which is also needed in the api url. So, is this possible with requests alone? Do I need to use something else entirely or a combination?. Thanks very much in advance.
import time
import requests
import urllib.parse
print("Welcome to Restaurant Search")
time.sleep(1)
headers = {'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36'}
address = input("Enter the City name :")
lat_long_url = 'https://nominatim.openstreetmap.org/search/' + urllib.parse.quote(address) +'?format=json'
lat_long = requests.get(url=lat_long_url, headers=headers).json()
time.sleep(3)
name = input("Enter the name of the Restaurant :")
search_url = f'https://www.swiggy.com/dapi/restaurants/search/v2_2?lat={lat_long[0]["lat"]}&lng={lat_long[0]["long"]}&trackingId=ebaedb8a-d86d-2e0b-69c8-4a3643e7a397&str={name}&sld=false&non_partner_search=false'
rest_url = f'https://www.swiggy.com/dapi/menu/v4/full?lat={lat_long[0]["lat"]}&lng={lat_long[0]["lon"]}&menuId=41100'(This)
n = False
info = requests.get(search_url).json()
print(info)
restaurant = info["data"]['restaurants']
for v in restaurant:
restaurant_name = [v['restaurants']]
for v in restaurant_name:
for k in v:
if not n:
print("Restaurant Name: {}".format(k['name']))
print("City: {}".format(k['city']))
print("Area: {}".format(k['area']))
print("Ratings: {}".format(k['avgRating']))
n = True
Edit I tried it with the same trackingID. It works as of now, no idea if it will be an issue in the future. However, still no way to figure out the menuID part.
INFO ..when I send a request to https://www.swiggy.com/dapi/menu/v4/full? It says it needs a slug or a menuId. The original response has a `slug like so..''slugs': {'restaurant': 'pebble-street-new-friends-colony-lajpat-nagar', 'city': 'delhi'}' . So, I guess I will try to see how to put this in the request.cannot find out how as of now