{ "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 : Easy Python – A programming language of revolution\n", "this is without html tags: Easy Python\n", "Skip to content\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", "Skip to content\n", "\n", "
\n", "
\n", "Search\n", "Easy Python\n", "Home\n", "Contact\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": [ "\n", "
\n", "
\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": [ "
\n", "\n", "['cover']\n", "\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": [ "World War II - Wikipedia\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 }