SHARE
TWEET

Untitled

a guest Apr 20th, 2019 161 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. page = requests.get(url)
  2. soup = bs(page.content, 'html.parser')
  3.  
  4. # Get data from header
  5. forecast_table = soup.find('table', attrs={'class': 'forecast__table forecast__table--js'})
  6. days = forecast_table.find('tr', attrs={'data-row': 'days'}).find_all('td')
  7.  
  8. # Get rows from body
  9. times = forecast_table.find('tr', attrs={'data-row': 'time'}).find_all('td')
  10. winds = forecast_table.find('tr', attrs={'data-row': 'wind'}).find_all('img')  # Use "img" instead of "td" to get direction of wind
  11. summaries = forecast_table.find('tr', attrs={'data-row': 'summary'}).find_all('td')
  12. rains = forecast_table.find('tr', attrs={'data-row': 'rain'}).find_all('td')
  13. snows = forecast_table.find('tr', attrs={'data-row': 'snow'}).find_all('td')
  14. max_temps = forecast_table.find('tr', attrs={'data-row': 'max-temperature'}).find_all('td')
  15. min_temps = forecast_table.find('tr', attrs={'data-row': 'min-temperature'}).find_all('td')
  16. chills = forecast_table.find('tr', attrs={'data-row': 'chill'}).find_all('td')
  17. freezings = forecast_table.find('tr', attrs={'data-row': 'freezing-level'}).find_all('td')
  18. sunrises = forecast_table.find('tr', attrs={'data-row': 'sunrise'}).find_all('td')
  19. sunsets = forecast_table.find('tr', attrs={'data-row': 'sunset'}).find_all('td')
  20.  
  21. # Iterate over days
  22. for i, day in enumerate(days):
  23.     current_day = clean(day.get_text())
  24.     elevation = url.rsplit('/', 1)[-1]
  25.     num_cols = int(day['data-columns'])
  26.  
  27.     if current_day != '':
  28.  
  29.         date = str(datetime.date(datetime.date.today().year, datetime.date.today().month, int(current_day.split(' ')[1])))  # Avoid using date format. Pandas adds 00:00:00 for some reason. Figure out better way to format
  30.  
  31.         # Iterate over forecast
  32.         for j in range(i, i + num_cols):    
  33.  
  34.             time_cell = clean(times[j].get_text())
  35.             wind = clean(winds[j]['alt'])
  36.             summary = clean(summaries[j].get_text())
  37.             rain = clean(rains[j].get_text())
  38.             snow = clean(snows[j].get_text())
  39.             max_temp = clean(max_temps[j].get_text())
  40.             min_temp = clean(min_temps[j].get_text())
  41.             chill = clean(chills[j].get_text())
  42.             freezing = clean(freezings[j].get_text())
  43.             sunrise = clean(sunrises[j].get_text())
  44.             sunset = clean(sunsets[j].get_text())
  45.  
  46.             rows.append(np.array([mountain_name, date, elevation, time_cell, wind, summary, rain, snow, max_temp, min_temp, chill, freezing, sunrise, sunset]))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top