|
24 | 24 | }, |
25 | 25 | { |
26 | 26 | "cell_type": "code", |
27 | | - "execution_count": null, |
| 27 | + "execution_count": 1, |
28 | 28 | "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 | + ], |
30 | 39 | "source": [ |
31 | 40 | "# Import required libraries for web scraping\n", |
32 | 41 | "print(\"📚 Setting up the environment...\")\n", |
|
48 | 57 | }, |
49 | 58 | { |
50 | 59 | "cell_type": "code", |
51 | | - "execution_count": null, |
| 60 | + "execution_count": 5, |
52 | 61 | "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 | + ], |
54 | 75 | "source": [ |
55 | 76 | "# First, let's try without headers to demonstrate why they're needed\n", |
56 | 77 | "print(\"🌐 Attempting to access webpage without headers...\")\n", |
57 | 78 | "\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", |
59 | 80 | "\n", |
60 | 81 | "try:\n", |
61 | 82 | " response = requests.get(url)\n", |
|
80 | 101 | }, |
81 | 102 | { |
82 | 103 | "cell_type": "code", |
83 | | - "execution_count": null, |
| 104 | + "execution_count": 6, |
84 | 105 | "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 | + ], |
86 | 117 | "source": [ |
87 | 118 | "# Now let's try with proper headers\n", |
88 | 119 | "print(\"🌐 Attempting to access webpage with proper headers...\")\n", |
|
105 | 136 | " print(f\"❌ Error: {str(e)}\")" |
106 | 137 | ] |
107 | 138 | }, |
| 139 | + { |
| 140 | + "cell_type": "markdown", |
| 141 | + "metadata": {}, |
| 142 | + "source": [ |
| 143 | + "Install https://selectorgadget.com/ to extract different parts of the website." |
| 144 | + ] |
| 145 | + }, |
108 | 146 | { |
109 | 147 | "cell_type": "markdown", |
110 | 148 | "metadata": {}, |
|
118 | 156 | "cell_type": "code", |
119 | 157 | "execution_count": null, |
120 | 158 | "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 | + ], |
122 | 180 | "source": [ |
123 | 181 | "# Parse the HTML and extract content\n", |
124 | 182 | "print(\"🔍 Parsing webpage content...\")\n", |
|
137 | 195 | " # Extract paragraphs\n", |
138 | 196 | " print(\"\\n📝 Article Content:\")\n", |
139 | 197 | " 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", |
141 | 199 | " texts = [text.get_text().strip() for text in description_html]\n", |
142 | 200 | " text = \"\\n\".join(texts)\n", |
143 | 201 | " print(text)\n", |
|
165 | 223 | "name": "python", |
166 | 224 | "nbconvert_exporter": "python", |
167 | 225 | "pygments_lexer": "ipython3", |
168 | | - "version": "3.8.0" |
| 226 | + "version": "3.13.5" |
169 | 227 | } |
170 | 228 | }, |
171 | 229 | "nbformat": 4, |
|
0 commit comments