1+ import requests
2+ from bs4 import BeautifulSoup as bs
3+
4+ def get_restraunt_details (restraunt_url ):
5+ """
6+ ```python
7+ get_restraunt_details(restraunt_url="https://www.swiggy.com/restaurants/pizza-hut-western-extension-area-karol-bagh-delhi-435678")
8+ ```
9+ Response
10+ ```js
11+ {
12+ "name":"Pizza Hut",
13+ "cuisine":"Pizzas",
14+ "area":"Karol Bagh",
15+ "rating":"3.7",
16+ "rating_count":"1K+ ratings",
17+ "cost_per_person":"₹350 for two",
18+ "offers":[
19+ {
20+ "15% OFF UPTO ₹300":"USE CITIFOODIE | ABOVE ₹1200"
21+ }
22+ ...
23+ ]
24+ }
25+ ```
26+ """
27+ try :
28+ headers = {
29+ "User-Agent" : "Mozilla/5.0 (Windows NT 6.3; Win 64 ; x64) Apple WeKit /537.36(KHTML , like Gecko) Chrome/80.0.3987.162 Safari/537.36"
30+ }
31+ response = requests .get (restraunt_url , headers = headers ).text
32+ soup = bs (response , "lxml" )
33+ restaurant_data = []
34+ name = soup .find (
35+ "p" , {"class" : "RestaurantNameAddress_name__2IaTv" }
36+ ).text .strip ()
37+ cuisine = soup .find (
38+ "p" , {"class" : "RestaurantNameAddress_cuisines__mBHr2" }
39+ ).text .strip ()
40+ area = soup .find (
41+ "p" , {"class" : "RestaurantNameAddress_area__2P9ib" }
42+ ).text .strip ()
43+ rating = soup .find (
44+ "span" , {"class" : "RestaurantRatings_avgRating__1TOWY" }
45+ ).text .strip ()
46+ rating_count = soup .find (
47+ "span" , {"class" : "RestaurantRatings_totalRatings__3d6Zc" }
48+ ).text .strip ()
49+ cost_per = soup .find_all ("li" , {"class" : "RestaurantTimeCost_item__2HCUz" })[
50+ - 1
51+ ].text .strip ()
52+ offers = []
53+ offer_box = soup .find_all (
54+ "div" , {"class" : "RestaurantOffer_infoWrapper__2trmg" }
55+ )
56+ for offer in offer_box :
57+ offer_ = {}
58+ offer_header = offer .find (
59+ "p" , {"class" : "RestaurantOffer_header__3FBtQ" }
60+ ).text .strip ()
61+ offer_content_1 = offer .find ("span" ).text .strip ()
62+ offer_content_2 = offer .find (
63+ "span" , {"class" : "RestaurantOffer_description__1SRJf" }
64+ ).text .strip ()
65+ offer_content = offer_content_1 + " | " + offer_content_2
66+ offer_ [offer_header ] = offer_content
67+ offers .append (offer_ )
68+ restaurant_data = {
69+ "name" : name ,
70+ "cuisine" : cuisine ,
71+ "area" : area ,
72+ "rating" : rating ,
73+ "rating_count" : rating_count ,
74+ "cost_per_person" : cost_per ,
75+ "offers" : offers ,
76+ }
77+ return restaurant_data
78+ except :
79+ return None
80+
81+ if __name__ == "__main__" :
82+ print (get_restraunt_details ("https://www.swiggy.com/restaurants/pizza-hut-western-extension-area-karol-bagh-delhi-435678" ))
0 commit comments