forked from AadheeshN/WebsiteDataScraper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper.py
More file actions
39 lines (34 loc) · 1.38 KB
/
scraper.py
File metadata and controls
39 lines (34 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Import Necessary Modules
import time
import csv
from selenium import webdriver
from bs4 import BeautifulSoup
# Link + Browser Info
start_url = 'https://exoplanets.nasa.gov/discovery/exoplanet-catalog/'
browser = webdriver.Chrome('chromedriver')
browser.get(start_url)
time.sleep(10)
# Function to Exract Information from Site
def scrape():
headers = ["name", "light_years_from_earth", "planet_mass", "stellar_magnitude", "discovery_date"]
planet_data = []
for i in range(0, 452):
soup = BeautifulSoup(browser.page_source, 'html.parser')
for ul_tag in soup.find_all('ul', attrs = {'class', 'exoplanet'}):
li_tags = ul_tag.find_all('li')
temp_list = []
for index, li_tag in enumerate(li_tags):
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)
browser.find_element_by_xpath('//*[@id="primary_column"]/footer/div/div/div/nav/span[2]/a').click()
with open("nasaData.csv", "w") as f:
csvwriter = csv.writer(f)
csvwriter.writerow(headers)
csvwriter.writerows(planet_data)
scrape()