<jupyter_start><jupyter_text>NASA Mars News<jupyter_code>url = "https://mars.nasa.gov/news/"
browser.visit(url)
html = browser.html
news_soup = BeautifulSoup(html, "html.parser")
mars_news = news_soup.select_one("ul.item_list li.slide")
mars_news.find("div", class_="content_title")
news_title = mars_news.find("div", class_="content_title").get_text()
print(news_title)
news_paragraph = mars_news.find("div", class_="article_teaser_body").get_text()
print(news_paragraph)<jupyter_output>Marvels of engineering, the rover's sample tubes must be tough enough to safely bring Red Planet samples on the long journey back to Earth in immaculate condition.<jupyter_text>JPL Mars Space Images - Featured Image<jupyter_code># Visit the NASA JPL (Jet Propulsion Laboratory) Site
executable_path = {"executable_path": "/Users/redea/.wdm/drivers/chromedriver/win32/87.0.4280.88/chromedriver"}
browser = Browser("chrome", **executable_path)
url = "https://www.jpl.nasa.gov/spaceimages/?search=&category=Mars"
browser.visit(url)
# <button class="full_image">Full Image</button>
full_image_button = browser.find_by_id("full_image")
full_image_button.click()
browser.is_element_present_by_text("more info", wait_time=1)
more_info_element = browser.find_link_by_partial_text("more info")
more_info_element.click()
# Parse Results HTML with BeautifulSoup
html = browser.html
image_soup = BeautifulSoup(html, "html.parser")

img_url = image_soup.select_one("figure.lede a img").get("src")
img_url
# Use Base URL to Create Absolute URL
img_url = f"https://www.jpl.nasa.gov{img_url}"
print(img_url)<jupyter_output>https://www.jpl.nasa.gov/spaceimages/images/largesize/PIA19913_hires.jpg<jupyter_text>Mars Facts<jupyter_code>def mars_facts():
    url = 'https://space-facts.com/mars/'
    browser.visit(url)

    mars_facts_df = pd.read_html(url)
    mars_facts_df = mars_facts_df[0]
    mars_facts_df.columns = ['Description', 'Mars']
    mars_facts_df

    mars_facts_html = mars_facts_df.to_html(classes='table table-striped')
    
    return mars_facts_html
# Visit the Mars Facts Site Using Pandas to Read
mars_df = pd.read_html("https://space-facts.com/mars/")[0]
print(mars_df)
mars_df.columns=["Description", "Value"]
mars_df.set_index("Description", inplace=True)
mars_df
# Visit the USGS Astrogeology Science Center Site
executable_path = {"executable_path": "/Users/redea/.wdm/drivers/chromedriver/win32/87.0.4280.88/chromedriver"}
browser = Browser("chrome", **executable_path, headless=False)
url = "https://astrogeology.usgs.gov/search/results?q=hemisphere+enhanced&k1=target&v1=Mars"
browser.visit(url)<jupyter_output><empty_output><jupyter_text>Mars Hemispheres<jupyter_code>hemisphere_image_urls = []

# Get a List of All the Hemispheres
links = browser.find_by_css("a.product-item h3")
for item in range(len(links)):
    hemisphere = {}
    
    # Find Element on Each Loop to Avoid a Stale Element Exception
    browser.find_by_css("a.product-item h3")[item].click()
    
    # Find Sample Image Anchor Tag & Extract <href>
    sample_element = browser.find_link_by_text("Sample").first
    hemisphere["img_url"] = sample_element["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
    browser.back()
hemisphere_image_urls<jupyter_output>C:\Users\redea\anaconda3\lib\site-packages\splinter\driver\webdriver\__init__.py:498: FutureWarning: browser.find_link_by_text is deprecated. Use browser.links.find_by_text instead.
  warnings.warn(<jupyter_text>Insert into Mongo DB<jupyter_code>def scrape_all():

#     # Populate variables from the functions
#     news_title, news_p = mars_news()
#     featured_img_url = featured_image()
    mars_facts_html = mars_facts()

    # Assemble the document to insert into the database
    nasa_document = {
        'news_title': news_title,
        'news_paragraph': news_paragraph,
        'featured_img_url': img_url,
        'mars_facts_html': mars_facts_html,
        'mars_hemisphere': hemisphere_image_urls
    }

    return nasa_document
# Connect to MongoDB
conn = 'mongodb://localhost:27017'
client = pymongo.MongoClient(conn)

# Connect to mars_app database
db = client.mars_app

# Connect to mars collection
mars = db.mars

# Gather document to insert
data_document = scrape_all()

# Insert into the mars collection
# mars.insert_one(data_document)

# Upsert into the mars collection (preferred to avoid duplicates)
mars.update_one({}, {'$set': data_document}, upsert=True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11774891774891776, 'fraction_numerical': 0.009523809523809525, 'mean_word_length': 5.49929676511955, 'pattern_counts': {'":': 2, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 7, 'lorem ipsum': 0, 'www.': 3, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '178609', 'license': 'ADSL', 'n_tokens_mistral': 1664, 'n_tokens_neox': 1596, 'n_words': 305, 'path': '.ipynb_checkpoints/Mission to Mars-checkpoint.ipynb', 'repo_name': 'redeat17/Web_Scraping_Challenge', 'size': '4620'}