{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from splinter import Browser\n",
    "from bs4 import BeautifulSoup\n",
    "\n",
    "\n",
    "def init_browser():\n",
    "    # @NOTE: Replace the path with your actual path to the chromedriver\n",
    "    executable_path = {\"executable_path\": \"/usr/local/bin/chromedriver\"}\n",
    "    return Browser(\"chrome\", **executable_path, headless=False)\n",
    "\n",
    "\n",
    "def scrape_news():\n",
    "    try:\n",
    "        browser = init_browser()\n",
    "    \n",
    "        news_url = \"https://mars.nasa.gov/news/\"\n",
    "        browser.visit(news_url)\n",
    "\n",
    "        html = browser.html\n",
    "        soup = BeautifulSoup(html, \"html.parser\")\n",
    "\n",
    "        news_title = soup.find_all(\"div\", class_=\"content_title\")\n",
    "        news_p = soup.find_all('p')[0].get_text()\n",
    "\n",
    "        return mars_info\n",
    "    finally:\n",
    "        browser.quit()\n",
    "\n",
    "def scrape_image():\n",
    "    try:    \n",
    "        browser = init_browser()\n",
    "    \n",
    "        image_url = \"https://www.jpl.nasa.gov/spaceimages/?search=&category=Mars\"\n",
    "        browser.visit(image_url)\n",
    "\n",
    "        html = browser.html\n",
    "        soup = BeautifulSoup(html, \"html.parser\")\n",
    "\n",
    "        featured_image_url = soup.find('article')['style'].replace('background-image: url(','').replace(');', '')[1:-1]\n",
    "\n",
    "        url = \"https://www.jpl.nasa.gov\"\n",
    "\n",
    "        featured_image_url = url + featured_image_url\n",
    "        featured_image_url\n",
    "\n",
    "        return mars_info\n",
    "    finally:\n",
    "        browser.quit()\n",
    "def scrape_facts():\n",
    "    try:\n",
    "        browser = init_browser()\n",
    "    \n",
    "        mars_facts_url = \"https://space-facts.com/mars/\"\n",
    "        browser.visit(mars_facts_url)\n",
    "\n",
    "        html = browser.html\n",
    "        soup = BeautifulSoup(html, \"html.parser\")\n",
    "\n",
    "        mars_facts_df = tables[0]\n",
    "        mars_facts_df.rows = ['Equatorial Diameter', 'Polar Diameter', 'Mass', 'Moons', \n",
    "                  'Orbit Distance', 'Orbit Period', 'Surface Temperature', 'First Record', \n",
    "                  'Recorded By']\n",
    "        mars_facts_df\n",
    "\n",
    "        html_table = mars_facts_df.to_html()\n",
    "        html_table\n",
    "        mars_facts_df.to_html('table.html')\n",
    "    \n",
    "        return mars_info\n",
    "    finally:\n",
    "        browser.quit()\n",
    "def scrape_hemisphere():\n",
    "    try:\n",
    "        browser = init_browser()\n",
    "    \n",
    "        hemisphere_url= \"https://astrogeology.usgs.gov/search/results?q=hemisphere+enhanced&k1=target&v1=Mars\"\n",
    "        browser.visit(hemisphere_url)\n",
    "\n",
    "        html = browser.html\n",
    "        soup = BeautifulSoup(html, \"html.parser\")\n",
    "\n",
    "        items = soup.find_all('div', class_='item')\n",
    "\n",
    "        hemisphere_image_urls = []\n",
    "\n",
    "        hemispheres_main_url = 'https://astrogeology.usgs.gov'\n",
    "\n",
    "        for i in items: \n",
    "            title = i.find('h3').text\n",
    "\n",
    "            partial_img_url = i.find('a', class_='itemLink product-item')['href']\n",
    "\n",
    "            browser.visit(hemispheres_main_url + partial_img_url)\n",
    " \n",
    "            partial_img_html = browser.html\n",
    "    \n",
    "            soup = bs( partial_img_html, 'html.parser')\n",
    "    \n",
    "            img_url = hemispheres_main_url + soup.find('img', class_='wide-image')['src']\n",
    "     \n",
    "            hemisphere_image_urls.append({\"title\" : title, \"img_url\" : img_url})\n",
    "    \n",
    "        hemisphere_image_urls\n",
    "    \n",
    "        return mars_info\n",
    "    finally:\n",
    "        browser.quit()\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.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
