<jupyter_start><jupyter_text>NASA Mars News<jupyter_code># Use Splinter to visit the NASA news website to be scraped
# Scrape https://mars.nasa.gov/news/ and collect the latest News Title and Paragraph Text
url = 'https://mars.nasa.gov/news/'
browser.visit(url)
# Iterate through pages and pull articles
# Create HTML object   
html = browser.html

# Create BeautifulSoup object to parse using html.parser
soup_news = bs(html, 'html.parser')

# Use classes to extract information
# Assign the text to variables that you can reference later
news_title = soup_news.find('div', class_='list_text').find('div', class_='content_title').text
print(f'The most current article on NASA.gov is "{news_title}."')

# Extract the teaser paragraph for the first article
news_p = soup_news.find('div', class_='article_teaser_body').text
print(f'The article, "{news_title}," discusses how {news_p}')<jupyter_output>The most current article on NASA.gov is "AI Is Helping Scientists Discover Fresh Craters on Mars."
The article, "AI Is Helping Scientists Discover Fresh Craters on Mars," discusses how It's the first time machine learning has been used to find previously unknown craters on the Red Planet.<jupyter_text>JPL Mars Space Images<jupyter_code># Visit the url for JPL Featured Space Image
url = 'https://www.jpl.nasa.gov/spaceimages/?search=&category=Mars'
browser.visit(url)
# Use splinter to navigate the site and find the image url for the current Featured Mars Image
full_image = browser.find_by_id('full_image')
full_image.click()
# Find the more info button and click it
browser.is_element_present_by_text('more info', wait_time=1)
more_info = browser.links.find_by_partial_text('more info')
more_info.click()
# Parse the resulting html with soup
html = browser.html
soup_img = bs(html, 'html.parser')
# Assign the url string to a variable called `featured_image_url`
featured_image_url = soup_img.select_one('figure.lede a img').get("src")
featured_image_url<jupyter_output><empty_output><jupyter_text>Mars Facts<jupyter_code># Bring in link for Mars Facts scrape
# Visit the Mars Facts webpage https://space-facts.com/mars/
url = 'https://space-facts.com/mars/'
# Use Pandas to scrape the table containing facts about the planet including Diameter, Mass, etc.
table_df = pd.read_html(url)
table_df
# Save table to files and organize in dataframe
tables = table_df[0]

# Use Pandas to convert the data to a HTML table string
# Define column headers
tables.columns = ['Mars Data', 'Value']

# Save table to files
mars_facts = tables.to_html('mars_facts.html')

!open mars_facts.html

tables<jupyter_output><empty_output><jupyter_text>Mars Hemispheres<jupyter_code># Bring in Mars website to be scraped and visit URL
# USGS Astrogeology site https://astrogeology.usgs.gov/search/results?q=hemisphere+enhanced&k1=target&v1=Mars 
# Obtain high resolution images for each of Mar's hemispheres
url = 'https://astrogeology.usgs.gov/search/results?q=hemisphere+enhanced&k1=target&v1=Mars'
browser.visit(url)
# Create a list to hold the images and titles.
hemisphere_image_urls = []

# Write code to retrieve the image urls and titles for each hemisphere.
# First, get a list of all of the hemispheres
links = browser.find_by_css("a.product-item h3")

# Next, loop through those links, click the link, find the sample anchor, and return the href
for i in range(len(links)):
    hemisphere = {}
    
# Find the elements on each loop to avoid a stale element exception
    browser.find_by_css("a.product-item h3")[i].click()
    
# Find the Sample image anchor tag and extract the href
    sample_elem = browser.links.find_by_text('Sample').first
    sample_elem = browser.links.find_by_partial_text('Sample')
    sample_elem = browser.find_by_text('Sample').first
    hemisphere['img_url'] = sample_elem['href']
    
# Get Hemisphere title
    hemisphere['title'] = browser.find_by_css("h2.title").text
    
# Append hemisphere object to list
    hemisphere_image_urls.append(hemisphere)
    
#  Navigate backwards to iterate all 4 images
    browser.back()
print(len(links))
# Save Hemisphere information to a dataframe
# Iterate image list
hemisphere_image_urls
# Quit the browser
browser.quit()
# Save hemisphere urls to a dataframe
hemisphere_image_urls_df = pd.DataFrame(hemisphere_image_urls)
hemisphere_image_urls_df<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0934837669813493, 'fraction_numerical': 0.002302555836979047, 'mean_word_length': 5.435555555555555, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 7, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '35142', 'license': 'ADSL', 'n_tokens_mistral': 1397, 'n_tokens_neox': 1359, 'n_words': 441, 'path': 'Mission_to_Mars/mission_to_mars.ipynb', 'repo_name': 'ibeth92/web-scraping-challenge', 'size': '4343'}