Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Scrapping data from Booking.com #1117 #1139

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions dev-documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -1932,6 +1932,23 @@ weather=Indiatodayweather("Mumbai")
and humidity of the place. |
---

### Booking.com

```py
from scrape_up import Booking.com
```

Create an instance of `BookingScraper` class

```python
scraper = BookingScraper("London")
```

| Method | Details |
| ------------------------ | --------------------------------------------------------- |
| `get_hotels()` | Returns a list of hotels with their details. |
---

## Bayt

```python
Expand Down
3 changes: 3 additions & 0 deletions src/scrape_up/booking.com/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .bookingcom import BookingScraper

__all__ = ["BookingScraper"]
56 changes: 56 additions & 0 deletions src/scrape_up/booking.com/bookingcom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import requests
from bs4 import BeautifulSoup

class BookingScraper:
"""
A class to scrape data from Booking.com

Create an instance of `BookingScraper` class

```python
scraper = BookingScraper("London")
```

| Method | Details |
| ------------------------ | --------------------------------------------------------- |
| `get_hotels()` | Returns a list of hotels with their details. |
"""

def __init__(self, location):
self.location = location
self.url = f"https://www.booking.com/searchresults.html?ss={self.location.replace(' ', '+')}"
self.headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
}

def get_hotels(self):
response = requests.get(self.url, headers=self.headers)
if response.status_code != 200:
print(f"Failed to retrieve the webpage. Status code: {response.status_code}")
return []

soup = BeautifulSoup(response.content, "html.parser")
hotels = []

hotel_elements = soup.find_all("div", {"data-testid": "property-card"})

if not hotel_elements:
print("No hotel elements found. The structure of the page may have changed.")
return hotels

for hotel in hotel_elements:
try:
hotel_name = hotel.find("div", {"data-testid": "title"}).get_text(strip=True) if hotel.find("div", {"data-testid": "title"}) else "No name"
hotel_reviews = hotel.find("div", {"data-testid": "review-score"}).get_text(strip=True) if hotel.find("div", {"data-testid": "review-score"}) else "No reviews"

hotels.append({
"name": hotel_name,
"reviews": hotel_reviews
})
except Exception as e:
print(f"Error parsing hotel: {e}")
continue

return hotels