Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import psycopg2
- import sys
- import random,urllib.request,os, csv, datetime,re
- from bs4 import BeautifulSoup
- con = None
- station_id = ["001D0A00B36E", "001D0A00B27C", "001D0A00B37E", "001D0A00529A"]
- stations_weather_data = []
- for i in station_id:
- url = "http://www.weatherlink.com/mapgetdata.php?DID="
- full_url = url + i
- htmlfile = urllib.request.urlopen(full_url).read().decode('utf-8')
- print(htmlfile)
- data = htmlfile.split('|')
- weather_data = {}
- weather_data['weather station name'] = data[3]
- weather_data['date'] = data[4]
- weather_data['temperature'] = float(''.join(ele for ele in data[5] if ele.isdigit() or ele == '.'))
- weather_data['temp_unit'] = " ".join(re.findall("[a-zA-Z]+", data[5]))
- weather_data['humidity'] = int(''.join(ele for ele in data[6] if ele.isdigit() or ele == '.'))
- weather_data['hum_unit'] = " ".join(re.findall("[%]+", data[6]))
- temp_wind = data[7].replace(" ", "")
- if temp_wind.isalnum():
- weather_data['wind_speed_status'] = " ".join(re.findall("[a-zA-Z]+", data[7]))
- weather_data['wind_unit'] = 'nounit'
- else:
- weather_data['wind_speed'] = float(''.join(ele for ele in data[7] if ele.isdigit() or ele == '.'))
- weather_data['wind_unit'] = " ".join(re.findall("[a-zA-Z/]+", data[7]))
- weather_data['wind_direction'] = " ".join(re.findall("[a-zA-Z0-9]+", data[8][:-4]))
- weather_data['wind_dir_unit'] = "degree"
- if data[9][-3:] == 'hPa':
- weather_data['barometer'] = float(''.join(ele for ele in data[9] if ele.isdigit() or ele == '.'))
- weather_data['barometer_unit'] = " ".join(re.findall("[a-zA-Z\']+", data[9]))
- else:
- weather_data['barometer'] = float(''.join(ele for ele in data[9] if ele.isdigit() or ele == '.'))
- weather_data['barometer_unit'] = data[9][-1:]
- weather_data['place_id'] = i
- stations_weather_data.append(weather_data)
- final_weather_data = tuple(stations_weather_data)
- try:
- con = psycopg2.connect("dbname='NPCLWeatherForecast' user='postgres' host='localhost' password='postgres'")
- cur = con.cursor()
- cur.mogrify("""INSERT INTO weather_data(temperature,temp_unit,humidity,hum_unit,wind,wind_speed_status,wind_unit,wind_dir,wind_dir_unit,barometer,bar_unit,updated_on,station_id) VALUES (%(temperature)s, %(temp_unit)s, %(humidity)s, %(hum_unit)s, %(wind)s, %(wind_speed_status)s, %(wind_unit)s, %(wind_dir)s, %(wind_dir_unit)s, %(barometer)s, %(bar_unit)s, %(updated_on)s, %(station_id)s);""", final_weather_data)
- ver = cur.fetchone()
- print(ver)
- except psycopg2.DatabaseError as e:
- print('Error {}'.format(e))
- sys.exit(1)
- finally:
- if con:
- con.close()
Add Comment
Please, Sign In to add comment