-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
45 lines (37 loc) · 1.69 KB
/
Copy pathexample.py
File metadata and controls
45 lines (37 loc) · 1.69 KB
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
"""Scrape Google Hotels prices and ratings with Python via the Google Hotels Scraper on Apify.
pip install -r requirements.txt
set APIFY_TOKEN in your environment (or paste it below)
python example.py
"""
import csv
import os
from datetime import date, timedelta
from apify_client import ApifyClient
client = ApifyClient(os.environ.get("APIFY_TOKEN", "<YOUR_APIFY_TOKEN>"))
check_in = date.today() + timedelta(days=30)
run_input = {
"queries": ["hotels in Paris", "hotels in Lisbon"],
"checkIn": check_in.isoformat(),
"checkOut": (check_in + timedelta(days=3)).isoformat(),
"adults": 2,
"currency": "USD",
"maxResults": 20, # you pay per hotel, keep it small while testing
}
run = client.actor("rel8ble/google-hotels-scraper").call(run_input=run_input)
hotels = list(client.dataset(run["defaultDatasetId"]).iterate_items())
print(f"Got {len(hotels)} hotels")
# 4-star and up, rated 4.0+, with a price for these dates, cheapest first
picks = [h for h in hotels
if (h.get("hotelClass") or 0) >= 4
and (h.get("rating") or 0) >= 4.0
and h.get("pricePerNight") is not None]
picks.sort(key=lambda h: h["pricePerNight"])
fields = ["query", "name", "hotelClass", "rating", "reviewCount", "pricePerNight",
"totalPrice", "currency", "deal", "latitude", "longitude", "googleHotelsUrl"]
with open("google_hotels.csv", "w", newline="", encoding="utf-8") as f:
w = csv.DictWriter(f, fieldnames=fields, extrasaction="ignore")
w.writeheader()
w.writerows(picks)
for h in picks[:5]:
print(f"${h['pricePerNight']}/night {h['hotelClass']}* {h['rating']} {h['name']} {h.get('deal') or ''}")
print(f"Saved {len(picks)} hotels to google_hotels.csv")