-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheltenhamGolfclub_scrap.py
More file actions
154 lines (130 loc) · 5.72 KB
/
Copy pathcheltenhamGolfclub_scrap.py
File metadata and controls
154 lines (130 loc) · 5.72 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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import requests
from bs4 import BeautifulSoup
from datetime import datetime, timedelta
import pandas as pd
import logging
# Configure logging
logging.basicConfig(
filename="./cheltenham-Golfclub-Data/cheltenhamscraper.log",
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
)
class cheltenhamGolfClubScraper:
def __init__(self, base_url, course_url):
"""
Initialize the cheltenhamGolfClubScraper class with base_url and course_url.
"""
self.base_url = base_url
self.course_url = course_url
logging.info(
"cheltenhamGolfClubScraper initialized with base_url: %s and course_url: %s",
base_url,
course_url,
)
def get_course_status(self, feeid_value, course_name):
"""
Extract course status data for a specific course.
"""
logging.info("Getting course status for course: %s", course_name)
num_days = 5
course_data = {}
for i in range(num_days):
date = (datetime.now() + timedelta(days=i)).strftime("%Y-%m-%d")
url = f"{self.course_url}&selectedDate={date}&feeGroupId={feeid_value}"
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
booking_slots = soup.select("div[class*=row-time]")
slot_data = []
for slot in booking_slots:
fee_items = slot.select("div.fees-wrapper ul li")
fee_types = []
prices = []
for index, item in enumerate(fee_items):
price = item.find("span", class_="price").get_text(strip=True)
fee_type = item.get_text(strip=True).replace(price, "").strip()
prices.append(price)
fee_types.append(fee_type)
p_elements = slot.select("div.records-wrapper p.small")
taken_count = 0
available_count = 0
for p_element in p_elements:
text = p_element.get_text(strip=True)
if text == "Taken":
taken_count += 1
elif text == "Available":
available_count += 1
slot_tee_time = slot.find("h3").text
slot_tee = slot.find("h4").text
slot_data.append(
[
slot_tee_time,
slot_tee,
fee_types,
prices,
taken_count,
available_count,
]
)
course_data[date] = slot_data
self.export_to_excel(course_name, course_data)
def scrape_course_data(self):
"""
Scrape course data for all courses.
"""
logging.info("Scraping course data for all courses")
response = requests.get(self.base_url)
soup = BeautifulSoup(response.content, "html.parser")
fee_group_rows = soup.select("div[class*=feeGroupRow]")
for row in fee_group_rows:
feeid_value = row.get("data-feeid")
course_name = row.find("h3").text
self.get_course_status(feeid_value, course_name)
break
def export_to_excel(self, course_name, course_data):
"""
Export course data to Excel files.
"""
logging.info("Exporting course data to Excel for course: %s", course_name)
with pd.ExcelWriter(f"./cheltenham-Golfclub-Data/{course_name}.xlsx") as writer:
for date, data in course_data.items():
df_data = []
fee_types_set = set()
prices_dict = {}
for slot in data:
slot_tee_time = slot[0]
slot_tee = slot[1]
taken_count = slot[-2]
available_count = slot[-1]
prices = slot[3]
fee_types = slot[2]
for fee_type, price in zip(fee_types, prices):
fee_types_set.add(fee_type)
if fee_type not in prices_dict:
prices_dict[fee_type] = [price]
else:
prices_dict[fee_type].append(price)
# Append the slot information and prices to df_data
row = [slot_tee_time, slot_tee, taken_count, available_count]
for fee_type in fee_types_set:
row.append(
prices_dict.get(fee_type, [""])[0]
if len(prices_dict.get(fee_type, [""])) > 0
else ""
)
df_data.append(row)
# Create the DataFrame with dynamic column names based on fee types
columns = [
"slot_tee_time",
"slot_tee",
"taken_count",
"available_count",
] + list(fee_types_set)
df = pd.DataFrame(df_data, columns=columns)
df.to_excel(writer, sheet_name=date, index=False)
# Define the base URL for the Cheltenham Golf Club booking page
base_url = "https://cheltenhamgolf.com.au/guests/bookings/ViewPublicCalendar.msp?booking_resource_id=3000000"
course_url = "https://cheltenhamgolf.com.au/guests/bookings/ViewPublicTimesheet.msp?bookingResourceId=3000000"
# Create an instance of the cheltenhamGolfClubScraper class
scraper = cheltenhamGolfClubScraper(base_url, course_url)
# Call the scrape_course_data method to initiate the scraping process and export the data to Excel files
scraper.scrape_course_data()