from splinter import Browser
from bs4 import BeautifulSoup as soup
from webdriver_manager.chrome import ChromeDriverManager
import pandas as pd

from PIL import Image

executable_path = {'executable_path':ChromeDriverManager().install()}
browser = Browser('chrome',**executable_path,headless=False)

# Visit the mars nasa news site
url = 'https://redplanetscience.com'
browser.visit(url)

# Optional delay for loading the page
# 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)

html=browser.html
news_soup = soup(html,'html.parser')
slide_elem = news_soup.select_one('div.list_text')

# 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


# ### Featured Images

#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()

html=browser.html
img_soup = soup(html,'html.parser')

# Find the relative image url
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

df = pd.read_html('https://galaxyfacts-mars.com')[0]
df.columns = ['Description', 'Mars','Earth']
df.set_index('Description',inplace=True)
df

df.to_html()


# ## D1: Scrape High-Resolution Mars’ Hemisphere Images and Titles

# ### Hemispheres

# 1. Use browser to visit the URL 
url = 'https://marshemispheres.com/'
browser.visit(url)

html=browser.html
hemi_img_soup = soup(html,'html.parser')

#click on the HTML hyperlink on the title of the photo to open the detailed page which contains the jpg link
img_elem=browser.find_by_css("a.itemLink.product-item h3")[2]
img_elem.text

img_elem.click()
# from the newly opened windowd, click on the 
list_elem=browser.links.find_by_text("Sample")[0]["href"]
list_elem

browser.back()

# 2. Create a list to hold the images and titles.
hemisphere_image_urls = []


# Find the count of all images
img_lst = hemi_img_soup.find_all(class_="item")
img_len = len(img_lst)

# 3. Write code to retrieve the image urls and titles for each hemisphere.
for i in range(img_len):
    
    hemisphere={}
    
    img_elem = browser.find_by_css("a.itemLink.product-item h3")[i]
    img_title = img_elem.text
    
    img_elem.click()
    img_url=browser.links.find_by_text("Sample")[0]["href"]
    
    hemisphere["img_url"] = img_url
    hemisphere["img_title"] = img_title
    
    hemisphere_image_urls.append(hemisphere)
    
    browser.back()

hemisphere_image_urls


# In[21]:


# 4. Print the list that holds the dictionary of each image url and title.
hemisphere_image_urls


# In[22]:


# 5. Quit the browser
browser.quit()


# In[ ]:




