반응형
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()반응형
'잡동사니' 카테고리의 다른 글
| 추석연휴 가족들과 같이 오크밸리 여행 (0) | 2023.10.01 |
|---|---|
| 만보걷기 2023.10.01 (0) | 2023.10.01 |
| 정규표현식 연습 사이트 (0) | 2022.08.04 |
| 데이터베이스 문제풀이 사이트(프로그래머스 사이트 중) (0) | 2022.08.04 |
| MySQL로 배우는 데이터베이스 개론과 실습 참고 사이트 (0) | 2022.07.17 |



