<jupyter_start><jupyter_text>Scrape<jupyter_code>from splinter import Browser
from bs4 import BeautifulSoup as bs
import pandas as pd
import time
executable_path = {"executable_path": "/Users/saleh/Downloads/chromedriver"}
browser = Browser("chrome", **executable_path, headless=False)<jupyter_output><empty_output><jupyter_text>Mars News<jupyter_code># Open browser to NASA Mars News Site
browser.visit('https://mars.nasa.gov/news/')
html = browser.html
soup = bs(html, 'html.parser')

# Search for news titles
title_results = soup.find_all('div', class_='content_title')

# Search for paragraph text under news titles
p_results = soup.find_all('div', class_='article_teaser_body')

# Extract first title and paragraph, and assign to variables
news_title = title_results[0].text
news_p = p_results[0].text

print(news_title)
print(news_p)<jupyter_output>Space Samples Link NASA's Apollo 11 and Mars 2020
While separated by half a century, NASA's Apollo 11 and Mars 2020 missions share the same historic goal: returning samples to Earth.<jupyter_text>Mars Featured Imaged<jupyter_code># Open browser to JPL Featured Image
browser.visit('https://www.jpl.nasa.gov/spaceimages/?search=&category=Mars')
# Click through to find full image
browser.click_link_by_partial_text('FULL IMAGE')
# Click again for full large image
time.sleep(2)
browser.click_link_by_partial_text('more info')
html = browser.html
soup = bs(html, 'html.parser')

# Search for image source
results = soup.find_all('figure', class_='lede')
relative_img_path = results[0].a['href']
featured_img = 'https://www.jpl.nasa.gov' + relative_img_path

print(featured_img)<jupyter_output>https://www.jpl.nasa.gov/spaceimages/images/largesize/PIA16883_hires.jpg<jupyter_text>Mars Weather<jupyter_code># Open browser to Mars Weather Twitter Account
browser.visit('https://twitter.com/marswxreport?lang=en')
html = browser.html
soup = bs(html, 'html.parser')

# Search for tweets
results = soup.find_all('p', class_="TweetTextSize")

# Grab text of first tweet
mars_weather = results[0].text

print(mars_weather)<jupyter_output>I’d say a plutonium-238 powered RTG qualifies the Curiosity and Mars2020 rovers as alternative fuel vehicles. You can explore these and other missions, rockets and more with JPL’s Spacecraft AR for IOS and Androhttps://www.jpl.nasa.gov/apps/pic.twitter.com/f4SheTlQyY<jupyter_text>Mars Facts<jupyter_code># Use Pandas to scrape data
tables = pd.read_html('https://space-facts.com/mars/')

# Take second table for Mars facts
df = tables[1]

# Rename columns and set index
df.columns=['description', 'value']
df
# Convert table to html
mars_facts_table = [df.to_html(classes='data table table-borderless', index=False, header=False, border=0)]
mars_facts_table<jupyter_output><empty_output><jupyter_text>Mars Hemispheres<jupyter_code># Open browser to USGS Astrogeology site
browser.visit('https://astrogeology.usgs.gov/search/results?q=hemisphere+enhanced&k1=target&v1=Mars')<jupyter_output><empty_output><jupyter_text>Search for hemisphere titles<jupyter_code>html = browser.html
soup = bs(html, 'html.parser')

hemi_names = []

# Search for the names of all four hemispheres
results = soup.find_all('div', class_="collapsible results")
hemispheres = results[0].find_all('h3')

# Get text and store in list
for name in hemispheres:
    hemi_names.append(name.text)

hemi_names<jupyter_output><empty_output><jupyter_text>Click through thumbnails to get full-sized hemisphere images<jupyter_code># Search for thumbnail links
thumbnail_results = results[0].find_all('a')
thumbnail_links = []

for thumbnail in thumbnail_results:
    
    # If the thumbnail element has an image...
    if (thumbnail.img):
        
        # then grab the attached link
        thumbnail_url = 'https://astrogeology.usgs.gov/' + thumbnail['href']
        
        # Append list with links
        thumbnail_links.append(thumbnail_url)

thumbnail_links<jupyter_output><empty_output><jupyter_text>Extract image source of full-sized image<jupyter_code>full_imgs = []

for url in thumbnail_links:
    
    # Click through each thumbanil link
    browser.visit(url)
    
    html = browser.html
    soup = bs(html, 'html.parser')
    
    # Scrape each page for the relative image path
    results = soup.find_all('img', class_='wide-image')
    relative_img_path = results[0]['src']
    
    # Combine the reltaive image path to get the full url
    img_link = 'https://astrogeology.usgs.gov/' + relative_img_path
    
    # Add full image links to a list
    full_imgs.append(img_link)

full_imgs<jupyter_output><empty_output><jupyter_text>Store as a list of dictionaries<jupyter_code># Zip together the list of hemisphere names and hemisphere image links
mars_hemi_zip = zip(hemi_names, full_imgs)

hemisphere_image_urls = []

# Iterate through the zipped object
for title, img in mars_hemi_zip:
    
    mars_hemi_dict = {}
    
    # Add hemisphere title to dictionary
    mars_hemi_dict['title'] = title
    
    # Add image url to dictionary
    mars_hemi_dict['img_url'] = img
    
    # Append the list with dictionaries
    hemisphere_image_urls.append(mars_hemi_dict)

hemisphere_image_urls<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10138781804163455, 'fraction_numerical': 0.007517347725520432, 'mean_word_length': 5.297330097087379, 'pattern_counts': {'":': 1, '<': 38, '<?xml version=': 0, '>': 38, 'https://': 10, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '326564', 'license': 'ADSL', 'n_tokens_mistral': 1800, 'n_tokens_neox': 1725, 'n_words': 430, 'path': 'mission_to_mars.ipynb', 'repo_name': 'salehakhatun/Web-Scraping-Homework---Mission-to-Mars', 'size': '5188'}