Amazing-Python-Scripts

Форк
0
75 строк · 2.3 Кб
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
                )
56
                price = (
57
                    r.find("span", class_="cost_for_two")
58
                    .getText()
59
                    .encode("ascii", "ignore")
60
                    .decode()
61
                    .strip()
62
                )
63
                restaurant_data["restaurants"].append(
64
                    {
65
                        "restaurant": name,
66
                        "location": location,
67
                        "rating": rating,
68
                        "cuisine": cuisine,
69
                        "price": "Rs. " + price + " for two",
70
                    }
71
                )
72
            res_json = json.dumps(restaurant_data)
73
            return res_json
74
        except ValueError:
75
            return None
76

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.