Amazing-Python-Scripts
75 строк · 2.3 Кб
1import json2import requests3from bs4 import BeautifulSoup4
5
6class EazyDiner:7"""8Class - `EazyDiner`
9Example:
10```
11restaurants = EazyDiner(location="Delhi NCR")
12```
13Methods :
141. ``.getRestaurants() | Response - List of restraunts and its details.
15"""
16
17def __init__(self, location):18self.location = location19
20def getRestaurants(self):21"""22Class - `EazyDiner`
23Example:
24```
25del = EazyDiner("Delhi NCR") or del = EazyDiner("delhi-ncr")
26del.getRestaurants()
27```
28Returns:
29{
30"restaurant": restaurant name
31"location": location of restaurant
32"rating": rating
33"cuisine": cuisines provided
34"price": price for two people
35}
36"""
37url = (38"https://www.eazydiner.com/restaurants?location="39+ self.location.replace(" ", "-").replace(",", "").lower()40)41try:42res = requests.get(url)43soup = BeautifulSoup(res.text, "html.parser")44
45restaurant_data = {"restaurants": []}46
47restaurants = soup.select(".restaurant")48for r in restaurants:49name = r.find("h3", class_="res_name").getText().strip()50location = r.find("h3", class_="res_loc").getText().strip()51rating = r.find("span", class_="critic").getText().strip()52cuisine = (53r.find("div", class_="res_cuisine").getText().replace(54",", ", ")55)56price = (57r.find("span", class_="cost_for_two")58.getText()59.encode("ascii", "ignore")60.decode()61.strip()62)63restaurant_data["restaurants"].append(64{65"restaurant": name,66"location": location,67"rating": rating,68"cuisine": cuisine,69"price": "Rs. " + price + " for two",70}71)72res_json = json.dumps(restaurant_data)73return res_json74except ValueError:75return None76