I created a script to scrape this website and gather the eclipse data (date, type, and location):
class Eclipse():
def __init__(self, eclipse_date, eclipse_type, eclipse_location):
self._eclipse_date = eclipse_date
self._eclipse_type = eclipse_type
self._eclipse_location = eclipse_location
@property
def get_eclipse_date(self):
return self._eclipse_date
@property
def get_eclipse_type(self):
return self._eclipse_type
@property
def get_eclipse_location(self):
return self._eclipse_location
def __str__(self):
return "Date: {0} Type {1} Location: {2}" .format(self._eclipse_date,self._eclipse_type,self._eclipse_location)
Main class:
import requests
from bs4 import BeautifulSoup
from Eclipse import Eclipse
from collections import namedtuple
def generate_eclipse_data(eclipse_info):
eclipse_list = []
for info in eclipse_info:
eclipse_date_and_type = info.find('a').text
retrieved_date_and_type = generate_date_and_type(eclipse_date_and_type)
eclipse_location = info.find('p').text
retrieved_location_data = generate_location(eclipse_location)
eclipse_data = Eclipse(retrieved_date_and_type.date,retrieved_date_and_type.type,retrieved_location_data.location)
eclipse_list.append(eclipse_data)
return eclipse_list
def generate_location(info_to_clean):
clean_location = namedtuple("Clean_Location",["location"])
cleanup_location = info_to_clean.split(" New Features")[0]
location_data = clean_location(cleanup_location)
return location_data
def generate_date_and_type(info_to_split):
date_and_type = namedtuple("Date_Type", ["date", "type"])
eclipse_split_data = info_to_split.split("—")
eclipse_date = eclipse_split_data[0]
eclipse_type = eclipse_split_data[1].strip()
eclipse_data = date_and_type(eclipse_date,eclipse_type)
return eclipse_data
def write_data_to_file(filename, data):
with open(filename, 'w') as eclipse_writer:
for info in data:
eclipse_writer.write(str(info))
eclipse_writer.write("\n")
def main():
eclipse_time_date = requests.get("https://www.timeanddate.com/eclipse/")
soup = BeautifulSoup(eclipse_time_date.text, 'html.parser')
eclipse_info = soup.find_all("div", class_= "six columns art__eclipse-txt")
eclipse_filename = 'eclipse.txt'
if (len(eclipse_info) > 0):
eclipse_data = generate_eclipse_data(eclipse_info)
write_data_to_file(eclipse_filename, eclipse_data)
else:
print("No data available")
if __name__ == "__main__":
main()
Output:
Date: July 13, 2018
Type: Partial Solar Eclipse
Location: South in Australia, Pacific, Indian Ocean
Date: July 27, 2018
Type: Total Lunar Eclipse
Location: Much of Europe, Much of Asia, Australia, Africa, South in North America, South America, Pacific, Atlantic, Indian Ocean, Antarctica
Questions:
I tried to be as descriptive as I can when naming my variables and functions. Are they clear enough?
Can I improve the script any further?