
# Import Splinter and BeautifulSoup
from splinter import Browser
from bs4 import BeautifulSoup as soup
from webdriver_manager.chrome import ChromeDriverManager
import pandas as pd

# Set up Splinter
# Remember, set your executable path in the next cell
executable_path = {'executable_path': ChromeDriverManager().install()}
browser = Browser('chrome', **executable_path, headless=False)

# Then set up the URL for scraping.
# Visit the mars nasa news site
url = 'https://redplanetscience.com'
browser.visit(url)
# Optional delay for loading the page
# First, we're searching for elements with a specific combination of tag (div) and attribute (list_text).
# Second, we're also telling our browser to wait one second before searching for components.
#  The optional delay is useful because sometimes dynamic pages take a little while to load, especially if they are image-heavy.
browser.is_element_present_by_css('div.list_text', wait_time=1)

# Set up the HTML parser
# Convert the browser html to a soup object and then quit the browser
html = browser.html
news_soup = soup(html, 'html.parser')

# We've assigned slide_elem as the variable to look for the <div /> tag and its descendent (the other tags within the <div /> element)
# The . is used for selecting classes, such as list_text, so the code 'div.list_text' pinpoints the <div /> tag with the class of list_text.
slide_elem = news_soup.select_one('div.list_text')

# In this line of code, we chained .find onto our previously assigned variable, slide_elem.
# The data we're looking for is the content title, which is in a <div /> with a class of 'content_title'.
slide_elem.find('div', class_='content_title')

# Use the parent element to find the first `a` tag and save it as `news_title`
news_title = slide_elem.find('div', class_='content_title').get_text()
news_title

# Use the parent element to find the paragraph text
news_p = slide_elem.find('div', class_='article_teaser_body').get_text()
news_p

# # JPL Spaces Images Featured Image

# Visit URL
url = 'https://spaceimages-mars.com'
browser.visit(url)

# Find and click the full image button
full_image_elem = browser.find_by_tag('button')[1]
full_image_elem.click()

# Parse the resulting html with soup
html = browser.html
img_soup = soup(html, 'html.parser')

# Find the relative image url
# .get('src') pulls the link to the image.
img_url_rel = img_soup.find('img', class_='fancybox-image').get('src')
img_url_rel

# Use the base URL to create an absolute URL
img_url = f'https://spaceimages-mars.com/{img_url_rel}'
img_url

# # Mars Facts
# ### Scraping a table using Pandas

# Creating a new DataFrame from the HTML table.
# df = pd.read_htmldf = pd.read_html('https://galaxyfacts-mars.com')[0]
#   The Pandas function read_html() specifically searches for and returns a list of tables found in the HTML.
#   By specifying an index of 0, we're telling Pandas to pull only the first table it encounters, or the first item in the list.
#   Then, it turns the table into a DataFrame.
# df.columns=['description', 'Mars', 'Earth'] 
#   Here, we assign columns to the new DataFrame for additional clarity.
# df.set_index('description', inplace=True)
#   By using the .set_index() function, we're turning the Description column into the DataFrame's index.
#   inplace=True means that the updated index will remain in place, without having to reassign the DataFrame to a new variable.
df = pd.read_html('https://galaxyfacts-mars.com')[0]
df.columns=['description', 'Mars', 'Earth']
df.set_index('description', inplace=True)
df

# Convert DataFrame back into HTML-ready code using
df.to_html()

browser.quit()