import pymysql
import requests
from bs4 import BeautifulSoup
import schedule
dbURL = "127.0.0.1"
dbPort = 3306
dbUser = 'study'
dbPass = 'study'
conn = pymysql.connect(
host=dbURL, port=dbPort, user=dbUser, passwd=dbPass, db='jspdb',
charset='utf8', use_unicode=True
)
insert_weather = "insert into jspdb.weather (city,tmef,wf,tmn,tmx) values (%s, %s, %s, %s, %s)"
select_last_date = "select tmef from jspdb.weather order by tmef desc limit 1"
def job():
req = requests.get("http://www.weather.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=108")
html = req.text
soup = BeautifulSoup(html, 'lxml')
cur = conn.cursor()
cur.execute(select_last_date)
last_date = cur.fetchone()
conn.commit()
weather = {}
# city, tmef, wf, tmn, tmx
for i in soup.find_all('location'):
weather[i.find('city').text] = []
for j in i.find_all('data'):
temp = []
if(j.find('tmef').text > last_date):
temp.append(j.find('tmef').text)
temp.append(j.find('wf').text)
temp.append(j.find('tmn').text)
temp.append(j.find('tmx').text)
weather[i.find('city').string].append(temp)
for i in weather:
for j in weather[i]:
cur = conn.cursor()
cur.execute(insert_weather,(i,j[0],j[1],j[2],j[3]))
conn.commit()
schedule.every().day.at("06:00").do(job)
while True:
schedule.run_pending()