from selenium import webdriver
from bs4 import BeautifulSoup
import time
import csv
Start_url="https://exoplanets.nasa.gov/discovery/exoplanet-catalog/"
browswer= webdriver.Chrome(executable_path="chromedriver_win32\chromedriver.exe")
browswer.get(Start_url)
time.sleep(10)
def scarpe():
    header=["name", "light_years_from_earth", "planet_mass", "stellar_magnitude", "discovery_date"]
    planet_data = []
    for i in range(0,441):
        soup=BeautifulSoup(browswer.page_source,"html.parser")
        for ul_tag in soup.find_all("ul",attrs={"class","exoplanet"}):
            li_tag=ul_tag.find_all("li")
            temp_list=[]
            for index, li_tag in enumerate(li_tag):
                if index==0:
                    temp_list.append(li_tag.find_all("a")[0].contents[0])
                else:
                    try:
                        temp_list.append(li_tag.contents[0])
                    except:
                        temp_list.append("")
            planet_data.append(temp_list)
        browswer.find_element_by_xpath('//*[@id="primary_column"]/footer/div/div/div/nav/span[2]/a').click()
    with open("test.csv",'w')  as f:
        csvwriter=csv.writer(f)
        csvwriter.writerow(header)
        csvwriter.writerows(planet_data)
scarpe()




