Skip to content

Commit 9bc72e6

Browse files
committed
update crawling notebook
1 parent 5e6f232 commit 9bc72e6

1 file changed

Lines changed: 68 additions & 10 deletions

File tree

‎serverless/3_web_scraping.ipynb‎

Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,18 @@
2424
},
2525
{
2626
"cell_type": "code",
27-
"execution_count": null,
27+
"execution_count": 1,
2828
"metadata": {},
29-
"outputs": [],
29+
"outputs": [
30+
{
31+
"name": "stdout",
32+
"output_type": "stream",
33+
"text": [
34+
"📚 Setting up the environment...\n",
35+
"✅ Libraries imported successfully\n"
36+
]
37+
}
38+
],
3039
"source": [
3140
"# Import required libraries for web scraping\n",
3241
"print(\"📚 Setting up the environment...\")\n",
@@ -48,14 +57,26 @@
4857
},
4958
{
5059
"cell_type": "code",
51-
"execution_count": null,
60+
"execution_count": 5,
5261
"metadata": {},
53-
"outputs": [],
62+
"outputs": [
63+
{
64+
"name": "stdout",
65+
"output_type": "stream",
66+
"text": [
67+
"🌐 Attempting to access webpage without headers...\n",
68+
"📡 Response status code: 403\n",
69+
"❌ Access forbidden! This demonstrates why we need proper headers.\n",
70+
"ℹ️ Websites often block requests without proper User-Agent headers\n",
71+
" to prevent automated scraping.\n"
72+
]
73+
}
74+
],
5475
"source": [
5576
"# First, let's try without headers to demonstrate why they're needed\n",
5677
"print(\"🌐 Attempting to access webpage without headers...\")\n",
5778
"\n",
58-
"url = \"https://ceu.edu/article/2024-12-03/combining-data-science-society-thanika-haltrich-presidential-scholar-award\"\n",
79+
"url = \"https://www.ceu.edu/news/2025-11/cultivating-mindsets-and-practices-environmental-sustainability\"\n",
5980
"\n",
6081
"try:\n",
6182
" response = requests.get(url)\n",
@@ -80,9 +101,19 @@
80101
},
81102
{
82103
"cell_type": "code",
83-
"execution_count": null,
104+
"execution_count": 6,
84105
"metadata": {},
85-
"outputs": [],
106+
"outputs": [
107+
{
108+
"name": "stdout",
109+
"output_type": "stream",
110+
"text": [
111+
"🌐 Attempting to access webpage with proper headers...\n",
112+
"📡 Response status code: 200\n",
113+
"✅ Successfully retrieved the webpage! Received 59136 bytes of data.\n"
114+
]
115+
}
116+
],
86117
"source": [
87118
"# Now let's try with proper headers\n",
88119
"print(\"🌐 Attempting to access webpage with proper headers...\")\n",
@@ -105,6 +136,13 @@
105136
" print(f\"❌ Error: {str(e)}\")"
106137
]
107138
},
139+
{
140+
"cell_type": "markdown",
141+
"metadata": {},
142+
"source": [
143+
"Install https://selectorgadget.com/ to extract different parts of the website."
144+
]
145+
},
108146
{
109147
"cell_type": "markdown",
110148
"metadata": {},
@@ -118,7 +156,27 @@
118156
"cell_type": "code",
119157
"execution_count": null,
120158
"metadata": {},
121-
"outputs": [],
159+
"outputs": [
160+
{
161+
"name": "stdout",
162+
"output_type": "stream",
163+
"text": [
164+
"🔍 Parsing webpage content...\n",
165+
"\n",
166+
"📑 Page Title:\n",
167+
"----------------------------------------\n",
168+
"Cultivating Mindsets and Practices for Environmental Sustainability | Central European University\n",
169+
"----------------------------------------\n",
170+
"\n",
171+
"📝 Article Content:\n",
172+
"----------------------------------------\n",
173+
"\n",
174+
"----------------------------------------\n",
175+
"\n",
176+
"✅ Content extracted successfully\n"
177+
]
178+
}
179+
],
122180
"source": [
123181
"# Parse the HTML and extract content\n",
124182
"print(\"🔍 Parsing webpage content...\")\n",
@@ -137,7 +195,7 @@
137195
" # Extract paragraphs\n",
138196
" print(\"\\n📝 Article Content:\")\n",
139197
" print(\"-\" * 40)\n",
140-
" description_html = webpage.select(\"#block-system-main p\") # <----------------- !!! Our selector !!!\n",
198+
" description_html = webpage.select(\".col-lg-8\") # <----------------- !!! Our selector !!!\n",
141199
" texts = [text.get_text().strip() for text in description_html]\n",
142200
" text = \"\\n\".join(texts)\n",
143201
" print(text)\n",
@@ -165,7 +223,7 @@
165223
"name": "python",
166224
"nbconvert_exporter": "python",
167225
"pygments_lexer": "ipython3",
168-
"version": "3.8.0"
226+
"version": "3.13.5"
169227
}
170228
},
171229
"nbformat": 4,

0 commit comments

Comments
 (0)