forked from avinashkranjan/Amazing-Python-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestaurants.py
75 lines (69 loc) · 2.27 KB
/
restaurants.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import json
import requests
from bs4 import BeautifulSoup
class EazyDiner:
"""
Class - `EazyDiner`
Example:
```
restaurants = EazyDiner(location="Delhi NCR")
```
Methods :
1. ``.getRestaurants() | Response - List of restraunts and its details.
"""
def __init__(self, location):
self.location = location
def getRestaurants(self):
"""
Class - `EazyDiner`
Example:
```
del = EazyDiner("Delhi NCR") or del = EazyDiner("delhi-ncr")
del.getRestaurants()
```
Returns:
{
"restaurant": restaurant name
"location": location of restaurant
"rating": rating
"cuisine": cuisines provided
"price": price for two people
}
"""
url = (
"https://www.eazydiner.com/restaurants?location="
+ self.location.replace(" ", "-").replace(",", "").lower()
)
try:
res = requests.get(url)
soup = BeautifulSoup(res.text, "html.parser")
restaurant_data = {"restaurants": []}
restaurants = soup.select(".restaurant")
for r in restaurants:
name = r.find("h3", class_="res_name").getText().strip()
location = r.find("h3", class_="res_loc").getText().strip()
rating = r.find("span", class_="critic").getText().strip()
cuisine = (
r.find("div", class_="res_cuisine").getText().replace(
",", ", ")
)
price = (
r.find("span", class_="cost_for_two")
.getText()
.encode("ascii", "ignore")
.decode()
.strip()
)
restaurant_data["restaurants"].append(
{
"restaurant": name,
"location": location,
"rating": rating,
"cuisine": cuisine,
"price": "Rs. " + price + " for two",
}
)
res_json = json.dumps(restaurant_data)
return res_json
except ValueError:
return None