From 5aa8ba95864187d226e8d5ae4be2be0660a9a6d3 Mon Sep 17 00:00:00 2001 From: PeterM18 <69867559+PeterM18@users.noreply.github.com> Date: Fri, 1 Jul 2022 15:33:43 -0500 Subject: [PATCH 1/2] Published from Watson Studio. --- testNB.ipynb | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 testNB.ipynb diff --git a/testNB.ipynb b/testNB.ipynb new file mode 100644 index 0000000..8080270 --- /dev/null +++ b/testNB.ipynb @@ -0,0 +1,32 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": "# testNB Peterm" + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3.9", + "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.9.12" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} \ No newline at end of file From 7f314cbf0e86908294f06460cef79c9fdb43f689 Mon Sep 17 00:00:00 2001 From: PeterM18 <69867559+PeterM18@users.noreply.github.com> Date: Thu, 28 Jul 2022 15:06:42 -0500 Subject: [PATCH 2/2] webscrapingLab webscrapingLab Python Notebook --- webscrapingLab.ipynb | 1196 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1196 insertions(+) create mode 100644 webscrapingLab.ipynb diff --git a/webscrapingLab.ipynb b/webscrapingLab.ipynb new file mode 100644 index 0000000..dbb6ff7 --- /dev/null +++ b/webscrapingLab.ipynb @@ -0,0 +1,1196 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

\n", + " \n", + " \"Skills\n", + " \n", + "

\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# **Space X Falcon 9 First Stage Landing Prediction**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Web scraping Falcon 9 and Falcon Heavy Launches Records from Wikipedia\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Estimated time needed: **40** minutes\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In this lab, you will be performing web scraping to collect Falcon 9 historical launch records from a Wikipedia page titled `List of Falcon 9 and Falcon Heavy launches`\n", + "\n", + "[https://en.wikipedia.org/wiki/List_of_Falcon\\_9\\_and_Falcon_Heavy_launches](https://en.wikipedia.org/wiki/List_of_Falcon\\_9\\_and_Falcon_Heavy_launches?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDS0321ENSkillsNetwork26802033-2022-01-01)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![](https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DS0321EN-SkillsNetwork/labs/module\\_1\\_L2/images/Falcon9\\_rocket_family.svg)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Falcon 9 first stage will land successfully\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Several examples of an unsuccessful landing are shown here:\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "More specifically, the launch records are stored in a HTML table shown below:\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![](https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DS0321EN-SkillsNetwork/labs/module\\_1\\_L2/images/falcon9-launches-wiki.png)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Objectives\n", + "\n", + "Web scrap Falcon 9 launch records with `BeautifulSoup`:\n", + "\n", + "* Extract a Falcon 9 launch records HTML table from Wikipedia\n", + "* Parse the table and convert it into a Pandas data frame\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "First let's import required packages for this lab\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Collecting beautifulsoup4\n", + " Downloading beautifulsoup4-4.11.1-py3-none-any.whl (128 kB)\n", + "\u001b[2K \u001b[38;2;114;156;31m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m128.2/128.2 KB\u001b[0m \u001b[31m889.6 kB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m kB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m:01\u001b[0m\n", + "\u001b[?25hCollecting soupsieve>1.2\n", + " Downloading soupsieve-2.3.2.post1-py3-none-any.whl (37 kB)\n", + "Installing collected packages: soupsieve, beautifulsoup4\n", + "Successfully installed beautifulsoup4-4.11.1 soupsieve-2.3.2.post1\n", + "Requirement already satisfied: requests in /home/pm/Software/miniconda3/lib/python3.9/site-packages (2.27.1)\n", + "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /home/pm/Software/miniconda3/lib/python3.9/site-packages (from requests) (1.26.9)\n", + "Requirement already satisfied: idna<4,>=2.5 in /home/pm/Software/miniconda3/lib/python3.9/site-packages (from requests) (3.3)\n", + "Requirement already satisfied: certifi>=2017.4.17 in /home/pm/Software/miniconda3/lib/python3.9/site-packages (from requests) (2022.6.15)\n", + "Requirement already satisfied: charset-normalizer~=2.0.0 in /home/pm/Software/miniconda3/lib/python3.9/site-packages (from requests) (2.0.12)\n" + ] + } + ], + "source": [ + "!pip3 install beautifulsoup4\n", + "!pip3 install requests" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "import sys\n", + "\n", + "import requests\n", + "from bs4 import BeautifulSoup\n", + "import re\n", + "import unicodedata\n", + "import pandas as pd" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "and we will provide some helper functions for you to process web scraped HTML table\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "def date_time(table_cells):\n", + " \"\"\"\n", + " This function returns the data and time from the HTML table cell\n", + " Input: the element of a table data cell extracts extra row\n", + " \"\"\"\n", + " return [data_time.strip() for data_time in list(table_cells.strings)][0:2]\n", + "\n", + "def booster_version(table_cells):\n", + " \"\"\"\n", + " This function returns the booster version from the HTML table cell \n", + " Input: the element of a table data cell extracts extra row\n", + " \"\"\"\n", + " out=''.join([booster_version for i,booster_version in enumerate( table_cells.strings) if i%2==0][0:-1])\n", + " return out\n", + "\n", + "def landing_status(table_cells):\n", + " \"\"\"\n", + " This function returns the landing status from the HTML table cell \n", + " Input: the element of a table data cell extracts extra row\n", + " \"\"\"\n", + " out=[i for i in table_cells.strings][0]\n", + " return out\n", + "\n", + "\n", + "def get_mass(table_cells):\n", + " mass=unicodedata.normalize(\"NFKD\", table_cells.text).strip()\n", + " if mass:\n", + " mass.find(\"kg\")\n", + " new_mass=mass[0:mass.find(\"kg\")+2]\n", + " else:\n", + " new_mass=0\n", + " return new_mass\n", + "\n", + "\n", + "def extract_column_from_header(row):\n", + " \"\"\"\n", + " This function returns the landing status from the HTML table cell \n", + " Input: the element of a table data cell extracts extra row\n", + " \"\"\"\n", + " if (row.br):\n", + " row.br.extract()\n", + " if row.a:\n", + " row.a.extract()\n", + " if row.sup:\n", + " row.sup.extract()\n", + " \n", + " colunm_name = ' '.join(row.contents)\n", + " \n", + " # Filter the digit and empty names\n", + " if not(colunm_name.strip().isdigit()):\n", + " colunm_name = colunm_name.strip()\n", + " return colunm_name \n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "To keep the lab tasks consistent, you will be asked to scrape the data from a snapshot of the `List of Falcon 9 and Falcon Heavy launches` Wikipage updated on\n", + "`9th June 2021`\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "static_url = \"https://en.wikipedia.org/w/index.php?title=List_of_Falcon_9_and_Falcon_Heavy_launches&oldid=1027686922\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Next, request the HTML page from the above URL and get a `response` object\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### TASK 1: Request the Falcon9 Launch Wiki page from its URL\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "First, let's perform an HTTP GET method to request the Falcon9 Launch HTML page, as an HTTP response.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "# use requests.get() method with the provided static_url\n", + "# assign the response to a object\n", + "reqObj = requests.get(static_url)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'\\n\\n\\n\\nList of Falcon 9 and Falcon Heavy launches - Wikipedia\\n