Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.34 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import requests
  4. from bs4 import BeautifulSoup
  5. import re
  6.  
  7. def get_soup(spot_id):
  8.     url = 'http://www.ndbc.noaa.gov/station_page.php?station='+spot_id
  9.     html_doc = (requests.get(url)).text
  10.     soup = BeautifulSoup(html_doc, 'html.parser')
  11.     return soup
  12.  
  13. def get_height(soup):
  14.     text = soup.get_text()
  15.     m = re.search(r"\(WVHT\)\:\n  (\d+.\d+) ft", text)
  16.     height = m.group(1)
  17.     return height
  18.  
  19. def get_period(soup):
  20.     text = soup.get_text()
  21.     m = re.search(r"\(DPD\)\:\n  (\d+) sec", text)
  22.     period = m.group(1)
  23.     return period
  24.  
  25. def get_direction(soup):
  26.     text = soup.get_text()
  27.     m = re.search(r"\(MWD\)\:\n(\w+) \( (\d+) deg", text)
  28.     direction = m.group(1)
  29.     return direction
  30.  
  31. def get_degrees(soup):
  32.     text = soup.get_text()
  33.     m = re.search(r"\(MWD\)\:\n(\w+) \( (\d+) deg", text)
  34.     degrees = m.group(2)
  35.     return degrees
  36.  
  37. def get_time(soup):
  38.     text = soup.get_text()
  39.     m = re.search(r"Conditions at \d+ as of\((\d+:\d+ \w+) \w+\)", text)
  40.     time = m.group(1)
  41.     return time
  42.  
  43.  
  44. def main():
  45.     id = '46225'
  46.     print 'height: ' + get_height(get_soup(id)) + '\nperiod: ' +get_period(get_soup(id)) + "\ndirection: "+get_direction(get_soup(id)) + "\ndegrees: " + get_degrees(get_soup(id)) + "\ntime: " + get_time(get_soup(id))
  47.  
  48.  
  49. if __name__ == '__main__':
  50.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement