|
| 1 | +import json |
| 2 | +import requests |
| 3 | +from bs4 import BeautifulSoup |
| 4 | + |
| 5 | + |
| 6 | +class EazyDiner: |
| 7 | + """ |
| 8 | + Class - `EazyDiner` |
| 9 | + Example: |
| 10 | + ``` |
| 11 | + restaurants = EazyDiner(location="Delhi NCR") |
| 12 | + ``` |
| 13 | + Methods : |
| 14 | + 1. ``.getRestaurants() | Response - List of restraunts and its details. |
| 15 | + """ |
| 16 | + |
| 17 | + def __init__(self, location): |
| 18 | + self.location = location |
| 19 | + |
| 20 | + def getRestaurants(self): |
| 21 | + """ |
| 22 | + Class - `EazyDiner` |
| 23 | + Example: |
| 24 | + ``` |
| 25 | + del = EazyDiner("Delhi NCR") or del = EazyDiner("delhi-ncr") |
| 26 | + del.getRestaurants() |
| 27 | + ``` |
| 28 | + Returns: |
| 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 | + """ |
| 37 | + url = ( |
| 38 | + "https://www.eazydiner.com/restaurants?location=" |
| 39 | + + self.location.replace(" ", "-").replace(",", "").lower() |
| 40 | + ) |
| 41 | + try: |
| 42 | + res = requests.get(url) |
| 43 | + soup = BeautifulSoup(res.text, "html.parser") |
| 44 | + |
| 45 | + restaurant_data = {"restaurants": []} |
| 46 | + |
| 47 | + restaurants = soup.select(".restaurant") |
| 48 | + for r in restaurants: |
| 49 | + name = r.find("h3", class_="res_name").getText().strip() |
| 50 | + location = r.find("h3", class_="res_loc").getText().strip() |
| 51 | + rating = r.find("span", class_="critic").getText().strip() |
| 52 | + cuisine = ( |
| 53 | + r.find("div", class_="res_cuisine").getText().replace(",", ", ") |
| 54 | + ) |
| 55 | + price = ( |
| 56 | + r.find("span", class_="cost_for_two") |
| 57 | + .getText() |
| 58 | + .encode("ascii", "ignore") |
| 59 | + .decode() |
| 60 | + .strip() |
| 61 | + ) |
| 62 | + restaurant_data["restaurants"].append( |
| 63 | + { |
| 64 | + "restaurant": name, |
| 65 | + "location": location, |
| 66 | + "rating": rating, |
| 67 | + "cuisine": cuisine, |
| 68 | + "price": "Rs. " + price + " for two", |
| 69 | + } |
| 70 | + ) |
| 71 | + res_json = json.dumps(restaurant_data) |
| 72 | + return res_json |
| 73 | + except ValueError: |
| 74 | + return None |
0 commit comments