In [301]:
# Import requests (for making HTTP requests).
# Import ast (for converting a string object into a list).
import requests
import ast

In [302]:
# Retrieve the content on the NASA homepage and convert to a standard string representation.
response = requests.get('https://www.nasa.gov/').content
r = response.decode('utf-8')

In [303]:
# Define the unique start and end keywords to surround the content of interest.
unique_start = '[{"type":"event","title":"Homepage Events","position":"3","links":'
unique_end = ',"launch":{'

In [304]:
# Create a substring slice in between the unique start and end keywords.
string_of_events = r[r.find(unique_start)+len(start):r.find(unique_end)]

In [305]:
# Convert the string into a list of dictionary entries.
list_of_events = ast.literal_eval(string_of_events)

In [306]:
# Remove key/value pairs that are not of interest
# Clean up escape character formatting of url
for event in list_of_events:
    del event['id']
    del event['target']
    event['url'] = event['url'].replace('\\','')

In [307]:
list_of_events


Out[307]:
[{'title': 'Wednesday, Jan. 4: Expedition 50 Spacewalk Preview Briefing from Johnson Space Center, 2 p.m. EST, NASA TV',
  'url': 'https://www.nasa.gov/press-release/nasa-preps-for-space-station-power-upgrade-spacewalks-live-nasa-tv-coverage'},
 {'title': 'Friday, Jan. 6: Shane Kimbrough and Peggy Wilson Expedition 50 Spacewalk, 7 a.m. EST (NASA TV Begins 5:30)',
  'url': 'https://www.nasa.gov/press-release/nasa-preps-for-space-station-power-upgrade-spacewalks-live-nasa-tv-coverage'},
 {'title': 'Friday, Jan. 13: Shane Kimbrough and Thomas Pesquet Expedition 50 Spacewalk, 7 a.m. EST (NASA TV Begins 5:30)',
  'url': 'https://www.nasa.gov/press-release/nasa-preps-for-space-station-power-upgrade-spacewalks-live-nasa-tv-coverage'},
 {'title': 'Registration Open: Super Bowl Week NASA Social at Johnson Space Center, Feb. 1',
  'url': 'https://www.nasa.gov/feature/go-behind-the-scenes-at-johnson-space-center-leading-up-to-super-bowl-li'}]

In [308]:
# Define the keyword of interest and print only those events containing the keyword.
keyword = 'Johnson Space Center'
list_of_interest = [event_info for event_info in list_of_events if keyword in event_info['title']]

In [312]:
list_of_interest


Out[312]:
[{'title': 'Wednesday, Jan. 4: Expedition 50 Spacewalk Preview Briefing from Johnson Space Center, 2 p.m. EST, NASA TV',
  'url': 'https://www.nasa.gov/press-release/nasa-preps-for-space-station-power-upgrade-spacewalks-live-nasa-tv-coverage'},
 {'title': 'Registration Open: Super Bowl Week NASA Social at Johnson Space Center, Feb. 1',
  'url': 'https://www.nasa.gov/feature/go-behind-the-scenes-at-johnson-space-center-leading-up-to-super-bowl-li'}]