riyadahmadov commited on
Commit
1064253
·
verified ·
1 Parent(s): 7141b8a

Upload Smapse_Data_Scrapt.ipynb

Browse files
Files changed (1) hide show
  1. Smapse_Data_Scrapt.ipynb +321 -0
Smapse_Data_Scrapt.ipynb ADDED
@@ -0,0 +1,321 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "73d78474",
6
+ "metadata": {},
7
+ "source": [
8
+ "<center><h1 style=\"background-color: darkblue; color: white; font-size: 30px;\"><b>👩🏻‍💻 Smapse Data Scrapt </b></h1></center>"
9
+ ]
10
+ },
11
+ {
12
+ "cell_type": "code",
13
+ "execution_count": null,
14
+ "id": "8f244628",
15
+ "metadata": {},
16
+ "outputs": [],
17
+ "source": [
18
+ "import requests\n",
19
+ "from bs4 import BeautifulSoup\n",
20
+ "import pandas as pd\n",
21
+ "\n",
22
+ "# Create an empty DataFrame to store the data\n",
23
+ "columns = ['Link', 'Name', 'Rating', 'Logo', 'Image', 'Description', 'Description1', 'Description2','Programs', 'Subject', 'Activities','Addresses','price','location_texts','li_texts']\n",
24
+ "smapse = pd.DataFrame(columns=columns)\n",
25
+ "\n",
26
+ "for i in range(701):\n",
27
+ " print(i)\n",
28
+ " url = f'https://smapse.com/catalog/?page={i}'\n",
29
+ " response = requests.get(url)\n",
30
+ " soup = BeautifulSoup(response.content, 'html.parser')\n",
31
+ "\n",
32
+ " div_tags = soup.find_all('div', class_='camp-min__head-name-inner')\n",
33
+ " for div in div_tags:\n",
34
+ " # Initialize lists to store data for each entry\n",
35
+ " name = []\n",
36
+ " rate = []\n",
37
+ " image = []\n",
38
+ " description = []\n",
39
+ " description1 = []\n",
40
+ " description2 = []\n",
41
+ " logo = []\n",
42
+ " subjects = []\n",
43
+ " activities = []\n",
44
+ " programs = []\n",
45
+ " price = []\n",
46
+ " addresses = []\n",
47
+ " location_texts = []\n",
48
+ " li_texts = []\n",
49
+ "\n",
50
+ "\n",
51
+ " \n",
52
+ " \n",
53
+ " links = div.find_all('a', href=True)\n",
54
+ " for link in links:\n",
55
+ " link1 = 'https://smapse.com/' + link['href']\n",
56
+ " url1 = link1\n",
57
+ " response1 = requests.get(url1)\n",
58
+ " soup1 = BeautifulSoup(response1.content, 'html.parser')\n",
59
+ " \n",
60
+ " # University name\n",
61
+ " university_name = soup1.find('h1').text.strip()\n",
62
+ " name.append(university_name)\n",
63
+ "\n",
64
+ " # Ratings\n",
65
+ " rating_div = soup1.find('div', class_='rating')\n",
66
+ " rating = rating_div.text.strip() if rating_div else None\n",
67
+ " rate.append(rating)\n",
68
+ "\n",
69
+ " # Images\n",
70
+ " gallery_div = soup1.find('div', class_='gallery__nav swiper-wrapper')\n",
71
+ " if gallery_div:\n",
72
+ " img_tags = gallery_div.find_all('img')\n",
73
+ " img_urls = ['https://smapse.com/' + img['src'] for img in img_tags]\n",
74
+ " image.extend(img_urls)\n",
75
+ "\n",
76
+ " # Description\n",
77
+ " description_div = soup1.find('div', class_='accordion')\n",
78
+ " if description_div:\n",
79
+ " h2_tags = description_div.find_all('h2', class_='accordion__title active')\n",
80
+ " description.append(h2_tags[0].text.strip())\n",
81
+ "\n",
82
+ " ul_tags = description_div.find_all('ul')\n",
83
+ " for ul in ul_tags:\n",
84
+ " li_tags = ul.find_all('li')\n",
85
+ " for li in li_tags:\n",
86
+ " description1.append(li.text.strip()) \n",
87
+ " description1_list = '|'.join(description1)\n",
88
+ "\n",
89
+ "\n",
90
+ "\n",
91
+ " div_tags = description_div.find_all('div', itemprop='description')\n",
92
+ " for div in div_tags:\n",
93
+ " p_tags = div.find_all('p')\n",
94
+ " for p in p_tags:\n",
95
+ " description2.append(p.text.strip() + ' ||| ')\n",
96
+ " description2_list = '|'.join(description2)\n",
97
+ "\n",
98
+ "\n",
99
+ " # Logo\n",
100
+ " logo_div = soup1.find('div', class_='side-logo')\n",
101
+ " if logo_div:\n",
102
+ " img_tag = logo_div.find('img', itemprop='logo')\n",
103
+ " if img_tag:\n",
104
+ " logo_url = 'https://smapse.com/' + img_tag['src']\n",
105
+ " logo.append(logo_url)\n",
106
+ " \n",
107
+ " # Programs\n",
108
+ " program_names = soup1.find_all('div', class_='program__name-info')\n",
109
+ " for program_name in program_names:\n",
110
+ " program = program_name.find('span', itemprop='name').text.strip()\n",
111
+ " programs.append(program)\n",
112
+ " \n",
113
+ " \n",
114
+ " # Subjects and Activities\n",
115
+ " subject_list = \"\"\n",
116
+ " activity_list = \"\"\n",
117
+ " span_boxes = soup1.find_all('div', class_='accordion__box')\n",
118
+ " for span_box in span_boxes:\n",
119
+ " strong_span = span_box.find('span', class_='strong')\n",
120
+ " if strong_span:\n",
121
+ " if \"Subject list\" in strong_span.text:\n",
122
+ " subject_list = span_box.text.strip().replace('Subject list:', '').strip()\n",
123
+ " # Check if \"Activity list\" exists in the subject list\n",
124
+ " if \"Activity list:\" in subject_list:\n",
125
+ " subject_list, activity_list = subject_list.split(\"Activity list:\", 1)\n",
126
+ " subject_list = subject_list.strip()\n",
127
+ " activity_list = activity_list.strip()\n",
128
+ " elif \"activity\" in strong_span.text:\n",
129
+ " activity_list = span_box.text.strip().replace('activity', '').strip()\n",
130
+ "\n",
131
+ " subjects.append(subject_list)\n",
132
+ " activities.append(activity_list)\n",
133
+ " \n",
134
+ " # Country\n",
135
+ " map_boxes = soup1.find_all('div', class_='map__box')\n",
136
+ "\n",
137
+ " # Iterate over each map box\n",
138
+ " for map_box in map_boxes:\n",
139
+ " # Find all anchor elements with class=\"map__address\" within the current map box\n",
140
+ " map_addresses = map_box.find_all('a', class_='map__address')\n",
141
+ "\n",
142
+ " # Extract the text from each anchor element and append it to the addresses list\n",
143
+ " for address in map_addresses:\n",
144
+ " addresses.append(address.text.strip())\n",
145
+ " \n",
146
+ " # city\n",
147
+ " location_items = soup1.find_all('div', class_='location__item')\n",
148
+ "\n",
149
+ " for location_item in location_items:\n",
150
+ " h_link_spans = location_item.find_all('span', class_='h-link')\n",
151
+ " for span in h_link_spans:\n",
152
+ " location_texts.append(span.text.strip())\n",
153
+ " \n",
154
+ " # Price \n",
155
+ " booking_prices = soup1.find('div', class_='booking__pries-new')\n",
156
+ " if booking_prices:\n",
157
+ " prices_content = booking_prices.get_text(strip=True)\n",
158
+ " price.append(prices_content) \n",
159
+ "\n",
160
+ " #regfee\n",
161
+ " accordion_div = soup1.find('div', class_=\"accordion__item\", id=\"school-about-tab-8\")\n",
162
+ " if accordion_div:\n",
163
+ " li_elements = accordion_div.find_all('li')\n",
164
+ " if li_elements:\n",
165
+ " li_texts = []\n",
166
+ " for li in li_elements:\n",
167
+ " li_texts.append(li.get_text(strip=True))\n",
168
+ " li_texts_combined = '|'.join(li_texts)\n",
169
+ "\n",
170
+ " \n",
171
+ " # Add data to DataFrame\n",
172
+ " smapse.loc[len(smapse)] = [link1, name, rate, logo, image, description, description1_list , description2_list,subjects,activities,programs,addresses,price,location_texts,li_texts_combined]\n",
173
+ " \n",
174
+ "smapse.columns = ['Link', 'Name', 'Rating', 'Logo', 'Image', 'Head',\n",
175
+ " 'ul_li', 'p_text','subject','activities','programs','address','price','location_texts','reg fee'] "
176
+ ]
177
+ },
178
+ {
179
+ "cell_type": "code",
180
+ "execution_count": null,
181
+ "id": "39e1bfc3",
182
+ "metadata": {},
183
+ "outputs": [],
184
+ "source": [
185
+ "smapse['text'] = smapse['p_text'].astype(str) + smapse['ul_li'].astype(str)\n",
186
+ "smapse['text'] = smapse['text'].apply(lambda x: str(x).replace('[', '').replace(']', '').replace('\\\\', '').replace('\"', ''))\n",
187
+ "smapse['text'] = smapse['text'].str.strip(\"'\")\n",
188
+ "smapse['text'] = smapse['text'].apply(lambda x: str(x))"
189
+ ]
190
+ },
191
+ {
192
+ "cell_type": "code",
193
+ "execution_count": null,
194
+ "id": "3164323a",
195
+ "metadata": {},
196
+ "outputs": [],
197
+ "source": [
198
+ "regions = pd.read_csv(r'C:\\Users\\eismayilov\\Desktop\\spamse\\regions.csv', encoding='latin-1')\n",
199
+ "school_types = pd.read_csv(r'C:\\Users\\eismayilov\\Desktop\\spamse\\school_types.csv', encoding='latin-1')\n",
200
+ "programs = pd.read_csv(r'C:\\Users\\eismayilov\\Desktop\\spamse\\programs.csv', encoding='latin-1')\n",
201
+ "counties = pd.read_csv(r'C:\\Users\\eismayilov\\Desktop\\spamse\\counties.csv', encoding='latin-1')\n",
202
+ "activities = pd.read_csv(r'C:\\Users\\eismayilov\\Desktop\\spamse\\activities.csv', encoding='latin-1')\n",
203
+ "subjects = pd.read_csv(r'C:\\Users\\eismayilov\\Desktop\\spamse\\subjects.csv', encoding='latin-1')\n",
204
+ "states = pd.read_csv(r'C:\\Users\\eismayilov\\Desktop\\spamse\\states.csv', encoding='latin-1')\n",
205
+ "sub_programs = pd.read_csv(r'C:\\Users\\eismayilov\\Desktop\\spamse\\sub_programs.csv', encoding='latin-1')\n",
206
+ "languages = pd.read_csv(r'C:\\Users\\eismayilov\\Desktop\\spamse\\languages.csv', encoding='latin-1')"
207
+ ]
208
+ },
209
+ {
210
+ "cell_type": "code",
211
+ "execution_count": null,
212
+ "id": "9e186ef8",
213
+ "metadata": {},
214
+ "outputs": [],
215
+ "source": [
216
+ "regions_list = regions['name_en'].to_list()\n",
217
+ "school_types_list = school_types['name_en'].to_list()\n",
218
+ "programs_list = programs['name_en'].to_list()\n",
219
+ "counties_list = counties['name_en'].to_list()\n",
220
+ "activities_list = activities['name_en'].to_list()\n",
221
+ "subjects_list = subjects['name_en'].to_list()\n",
222
+ "states_list = states['name_en'].to_list()\n",
223
+ "sub_programs_list = sub_programs['name_en'].to_list()\n",
224
+ "languages_list = languages['name_en'].to_list()"
225
+ ]
226
+ },
227
+ {
228
+ "cell_type": "code",
229
+ "execution_count": null,
230
+ "id": "d5634674",
231
+ "metadata": {},
232
+ "outputs": [],
233
+ "source": [
234
+ "# Function to find region in text and return it\n",
235
+ "for y, i in [('school_list',school_types_list),('regions_list',regions_list),('languages_list', languages_list), ('sub_programs_list', sub_programs_list), ('states_list', states_list), ('subjects_list', subjects_list), ('activities_list', activities_list), ('counties_list', counties_list), ('programs_list', programs_list)]:\n",
236
+ " def find_school(text):\n",
237
+ " for school in i:\n",
238
+ " if school.lower() in text.lower():\n",
239
+ " return school\n",
240
+ " return None\n",
241
+ "\n",
242
+ " # Apply the function to create a new column\n",
243
+ " smapse[y] = smapse['text'].apply(find_school)"
244
+ ]
245
+ },
246
+ {
247
+ "cell_type": "code",
248
+ "execution_count": null,
249
+ "id": "9425054e",
250
+ "metadata": {},
251
+ "outputs": [],
252
+ "source": [
253
+ "smapse.to_excel('smapse_school_data.xlsx', index = False)"
254
+ ]
255
+ },
256
+ {
257
+ "cell_type": "markdown",
258
+ "id": "40b2d964",
259
+ "metadata": {},
260
+ "source": [
261
+ "<h8><b style=\"color:darkblue; font-size: 18px;\">Dataset Description\n",
262
+ "\n",
263
+ "| Column Name | Description |\n",
264
+ "|-------------------------------|-----------------------------------------------------------------------------------|\n",
265
+ "| University Link | The URL or link associated with the university. |\n",
266
+ "| University Name | The name of the university. |\n",
267
+ "| University Rating | The rating or ranking of the university. |\n",
268
+ "| University Logo Link | The URL or link to the logo image of the university. |\n",
269
+ "| University Images Link Source | The URL or link to the source of images related to the university. |\n",
270
+ "| University Description Head | The heading or title of the university description. |\n",
271
+ "| University Descriptions | Detailed descriptions or information about the university. |\n",
272
+ "| Information about University | Additional information or details about the university. |\n",
273
+ "| University Subject | The main subjects or fields of study offered by the university. |\n",
274
+ "| University Activities | Activities or extracurricular programs offered by the university. |\n",
275
+ "| University Programs | Academic programs or courses offered by the university. |\n",
276
+ "| University Address | The physical address or location of the university. |\n",
277
+ "| Registration Fee | Fee required for registration or enrollment at the university. |\n",
278
+ "| Schools | Schools or faculties within the university. |\n",
279
+ "| Regions | Regions or areas associated with the university. |\n",
280
+ "| Languages | Languages of instruction or languages spoken at the university. |\n",
281
+ "| Sub Program | Subprograms or specialized areas of study within the university programs. |\n",
282
+ "| Price | Price or cost associated with university-related services or products. |\n",
283
+ "| Currency | Currency used for pricing or financial transactions. |\n",
284
+ "| Term | Term or semester information related to university programs. |\n",
285
+ "| Country | Country where the university is located. |\n",
286
+ "| City | City where the university is located. |\n",
287
+ "| University States | Additional states or information related to the university. |\n",
288
+ "</b></h8>"
289
+ ]
290
+ },
291
+ {
292
+ "cell_type": "markdown",
293
+ "id": "f29f222b",
294
+ "metadata": {},
295
+ "source": [
296
+ "<center><h1 style=\"background-color: darkblue; color: white; font-size: 30px;\"><b> 🔚The End </b></h1></center>"
297
+ ]
298
+ }
299
+ ],
300
+ "metadata": {
301
+ "kernelspec": {
302
+ "display_name": "Python 3 (ipykernel)",
303
+ "language": "python",
304
+ "name": "python3"
305
+ },
306
+ "language_info": {
307
+ "codemirror_mode": {
308
+ "name": "ipython",
309
+ "version": 3
310
+ },
311
+ "file_extension": ".py",
312
+ "mimetype": "text/x-python",
313
+ "name": "python",
314
+ "nbconvert_exporter": "python",
315
+ "pygments_lexer": "ipython3",
316
+ "version": "3.9.12"
317
+ }
318
+ },
319
+ "nbformat": 4,
320
+ "nbformat_minor": 5
321
+ }