File size: 10,930 Bytes
0ade82b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Requirements\n",
    "#pip3 install requests\n",
    "#pip3 install bs4"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Basic fundamentals of web scraping"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "this is with html tags : <title>Easy Python – A programming language of revolution</title>\n",
      "this is without html tags: Easy Python\n",
      "<a class=\"screen-reader-text skip-link\" href=\"#content\">Skip to content</a>\n"
     ]
    }
   ],
   "source": [
    "# import these two modules bs4 for selecting HTML tags easily\n",
    "from bs4 import BeautifulSoup\n",
    "# requests module is easy to operate some people use urllib but I prefer this one because it is easy to use.\n",
    "import requests\n",
    "\n",
    "# I put here my own blog url ,you can change it.\n",
    "url=\"https://getpython.wordpress.com/\"\n",
    "\n",
    "#Requests module use to data from given url\n",
    "source=requests.get(url)\n",
    "\n",
    "# BeautifulSoup is used for getting HTML structure from requests response.(craete your soup)\n",
    "soup=BeautifulSoup(source.text,'html')\n",
    "\n",
    "# Find function is used to find a single element if there are more than once it always returns the first element.\n",
    "title=soup.find('title') # place your html tagg in parentheses that you want to find from html.\n",
    "print(\"this is with html tags :\",title)\n",
    "\n",
    "qwery=soup.find('h1') # here i find first h1 tagg in my website using find operation.\n",
    "\n",
    "#use .text for extract only text without any html tags\n",
    "print(\"this is without html tags:\",qwery.text) \n",
    "\n",
    "\n",
    "links=soup.find('a') #i extarcted link using \"a\" tag\n",
    "print(links)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## extarct data from innerhtml "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "#content\n"
     ]
    }
   ],
   "source": [
    "# here i extarcted href data from anchor tag.\n",
    "print(links['href']) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['screen-reader-text', 'skip-link']\n"
     ]
    }
   ],
   "source": [
    "# similarly i got class details from a anchor tag\n",
    "print(links['class'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## findall operation in Bs4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "total links in my website : 37\n",
      "\n",
      "<a class=\"screen-reader-text skip-link\" href=\"#content\">Skip to content</a>\n",
      "<a href=\"https://getpython.wordpress.com/\" rel=\"home\">\n",
      "<div class=\"cover\"></div>\n",
      "</a>\n",
      "<a class=\"screen-reader-text search-toggle\" href=\"#search-container\">Search</a>\n",
      "<a href=\"https://getpython.wordpress.com/\" rel=\"home\">Easy Python</a>\n",
      "<a aria-current=\"page\" href=\"/\">Home</a>\n",
      "<a href=\"https://getpython.wordpress.com/contact/\">Contact</a>\n"
     ]
    }
   ],
   "source": [
    "# findall function is used to fetch all tags at a single time.\n",
    "many_link=soup.find_all('a') # here i extracted all the anchor tags of my website\n",
    "total_links=len(many_link) # len function is use to calculate length of your array\n",
    "print(\"total links in my website :\",total_links)\n",
    "print()\n",
    "for i in many_link[:6]: # here i use slicing to fetch only first 6 links from rest of them.\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<a href=\"https://getpython.wordpress.com/\" rel=\"home\">\n",
      "<div class=\"cover\"></div>\n",
      "</a>\n",
      "\n",
      "href is : https://getpython.wordpress.com/\n"
     ]
    }
   ],
   "source": [
    "second_link=many_link[1] #here i fetch second link which place on 1 index number in many_links.\n",
    "print(second_link)\n",
    "print()\n",
    "print(\"href is :\",second_link['href']) #only href link is extracted from ancor tag\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<div class=\"cover\"></div>\n",
      "\n",
      "['cover']\n",
      "<class 'list'>\n",
      "\n",
      "class name of div is : cover\n"
     ]
    }
   ],
   "source": [
    "# select div tag from second link\n",
    "nested_div=second_link.find('div')\n",
    "# As you can see div element extarcted , it also have inner elements\n",
    "print(nested_div)\n",
    "print()\n",
    "#here i extracted class element from div but it give us in the form of list\n",
    "z=(nested_div['class'])\n",
    "print(z)\n",
    "print(type(z))\n",
    "print()\n",
    "#  \" \" .join () method use to convert list type  into string type\n",
    "print(\"class name of div is :\",\" \".join(nested_div['class'])) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## scrap data from wikipedia"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<title>World War II - Wikipedia</title>\n"
     ]
    }
   ],
   "source": [
    "wiki=requests.get(\"https://en.wikipedia.org/wiki/World_War_II\")\n",
    "soup=BeautifulSoup(wiki.text,'html')\n",
    "print(soup.find('title'))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### find html tags with classes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Contents\n",
      "\n",
      "1 Chronology\n",
      "2 Background\n",
      "\n",
      "2.1 Europe\n",
      "2.2 Asia\n",
      "\n",
      "\n",
      "3 Pre-war events\n",
      "\n",
      "3.1 Italian invasion of Ethiopia (1935)\n",
      "3.2 Spanish Civil War (1936–1939)\n",
      "3.3 Japanese invasion of China (1937)\n",
      "3.4 Soviet–Japanese border conflicts\n",
      "3.5 European occupations and agreements\n",
      "\n",
      "\n",
      "4 Course of the war\n",
      "\n",
      "4.1 War breaks out in Europe (1939–40)\n",
      "4.2 Western Europe (1940–41)\n",
      "4.3 Mediterranean (1940–41)\n",
      "4.4 Axis attack on the Soviet Union (1941)\n",
      "4.5 War breaks out in the Pacific (1941)\n",
      "4.6 Axis advance stalls (1942–43)\n",
      "\n",
      "4.6.1 Pacific (1942–43)\n",
      "4.6.2 Eastern Front (1942–43)\n",
      "4.6.3 Western Europe/Atlantic and Mediterranean (1942–43)\n",
      "\n",
      "\n",
      "4.7 Allies gain momentum (1943–44)\n",
      "4.8 Allies close in (1944)\n",
      "4.9 Axis collapse, Allied victory (1944–45)\n",
      "\n",
      "\n",
      "5 Aftermath\n",
      "6 Impact\n",
      "\n",
      "6.1 Casualties and war crimes\n",
      "6.2 Genocide, concentration camps, and slave labour\n",
      "6.3 Occupation\n",
      "6.4 Home fronts and production\n",
      "6.5 Advances in technology and warfare\n",
      "\n",
      "\n",
      "7 See also\n",
      "8 Notes\n",
      "9 Citations\n",
      "10 References\n",
      "11 External links\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "ww2_contents=soup.find_all(\"div\",class_='toc')\n",
    "for i in ww2_contents:\n",
    "    print(i.text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "World War II(clockwise from top left)\n",
      "Chinese forces in the Battle of Wanjialing\n",
      "Australian 25-pounder guns during the First Battle of El Alamein\n",
      "German Stuka dive bombers on the Eastern Front in December 1943\n",
      "American naval force in the Lingayen Gulf\n",
      "Wilhelm Keitel signing the German Instrument of Surrender\n",
      "Soviet troops in the Battle of Stalingrad\n",
      "Date1 September 1939 – 2 September 1945 (1939-09-01 – 1945-09-02)(6 years and 1 day)[a]LocationEurope, Pacific, Atlantic, South-East Asia, China, Middle East, Mediterranean, North Africa, Horn of Africa, Australia, briefly North and South AmericaResult\n",
      "Allied victory\n",
      "Collapse of Nazi Germany\n",
      "Fall of the Japanese and Italian Empires\n",
      "Beginning of the Nuclear Age\n",
      "Dissolution of the League of Nations\n",
      "Creation of the United Nations\n",
      "Emergence of the United States and the Soviet Union as rival superpowers\n",
      "Beginning of the Cold War (more...)Participants\n",
      "Allies\n",
      "AxisCommanders and leaders\n",
      "Main Allied leaders\n",
      " Joseph Stalin\n",
      " Franklin D. Roosevelt\n",
      " Winston Churchill\n",
      " Chiang Kai-shek\n",
      "\n",
      "Main Axis leaders\n",
      " Adolf Hitler\n",
      " Hirohito\n",
      " Benito Mussolini\n",
      "Casualties and losses\n",
      "\n",
      "Military dead:\n",
      "Over 16,000,000\n",
      "Civilian dead:\n",
      "Over 45,000,000\n",
      "Total dead:\n",
      "Over 61,000,000\n",
      "(1937–1945)\n",
      "...further details\n",
      "\n",
      "\n",
      "Military dead:\n",
      "Over 8,000,000\n",
      "Civilian dead:\n",
      "Over 4,000,000\n",
      "Total dead:\n",
      "Over 12,000,000\n",
      "(1937–1945)\n",
      "...further details\n",
      "\n"
     ]
    }
   ],
   "source": [
    "overview=soup.find_all('table',class_='infobox vevent')\n",
    "for z in overview:\n",
    "    print(z.text)\n",
    "    "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}