Skip to content

Commit 3ec8054

Browse files
Merge pull request #2369 from Swapnil-Singh-99/added-swiggy-scraper
added a swiggy scraper module
2 parents c9a406d + 12c4f0f commit 3ec8054

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

Swiggy_Scraper/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## Swiggy Scraper
2+
This Python script will allow the user to scrape swiggy restaurants using there page links
3+
4+
## Running the script
5+
6+
1. Open the terminal in the directory
7+
2. Run: `python main.py`
8+
3. You get the details of the restaurant as results
9+
10+
## Connect with me
11+
12+
[![portfolio](https://img.shields.io/badge/github-000?style=for-the-badge&logo=github&logoColor=white)](https://github.com/Swapnil-Singh-99)
13+
[![linkedin](https://img.shields.io/badge/linkedin-0A66C2?style=for-the-badge&logo=linkedin&logoColor=white)](https://www.linkedin.com/in/swapnilsingh99/)

Swiggy_Scraper/main.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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"))

Swiggy_Scraper/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
beautifulsoup4==4.11.1
2+
Requests==2.31.0

0 commit comments

Comments
 (0)