그냥 대충 살겠어요.
나는 이 이야기를 무척 좋아한다...
그런 의미에서 이번 추가 과제도 좀 있다가 올리겠습니다...
# (p.431) Beautiful Soup_Flask
# 모듈 설정
from flask import Flask
from urllib import request
from bs4 import BeautifulSoup
# 웹 서버 생성
app = Flask(__name__)
@app.route("/")
def hello():
try:
target = request.urlopen("http://www.kma.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=108") # urlopen(): 기상청 전국 날씨 read
soup = BeautifulSoup(target, "html.parser") # BeautifulSoup: 웹 페이지 분석
output = "" # location tag 탐색
for location in soup.select("location"):
# 내부 city, wf, tmn, tmx tag 팀색 출력
city = location.select_one("city").string
wf = location.select_one("wf").string
tmn = location.select_one("tmn").string
tmx = location.select_one("tmx").string
output += f"<h3>{city}</h3>"
output += f"날씨: {wf}<br/>"
output += f"최저/최고 기온: {tmn}/{tmx}<br/>"
output += "<hr/>"
return output
except Exception as e:
return f"<h1>에러 발생: {e}</h1>" # 에러 발생 시 메시지 표시
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0')