-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
75 lines (60 loc) · 2.74 KB
/
app.py
File metadata and controls
75 lines (60 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from flask import Flask, render_template
from urllib import request
from bs4 import BeautifulSoup
app = Flask(__name__)
#1
# @app.route('/')
# def hello_world():
# return '<h1> Hello from Flask!! </h1>'
#1-1
@app.route('/<user>')
def hello(user):
return '<h1> hello ' + user
@app.route("/")
def home():
return render_template('index.html', subject="안녕하세요. 반갑습니다. 이민이입니다")
# 기상청 날씨
@app.route("/kma")
def kma():
# urlopen() 함수로 기상청의 전국 날씨를 읽습니다.
target = request.urlopen("http://www.kma.go.kr/weather/forecast/mid-term-rss3.jsp?stdId=108")
# BeautifulSoup를 사용해 웹 페이지를 분석합니다.
soup = BeautifulSoup(target, "html.parser")
output = ""
# item 태그를 찾습니다.
for item in soup.select("item"):
output += "<h2>{}</h2><hr/>".format(item.select_one("title").string)
# location 태그를 찾습니다.
for location in soup.select("location"):
# 내부의 city, wf, tmn, tmx 태그를 찾아 출력합니다.
output += "<h3>{}</h3>".format(location.select_one("city").string)
output += "날씨: {}</br>".format(location.select_one("wf").string)
output += "최저/최고 기온: {}/{}".format(location.select_one("tmn").string, location.select_one("tmx").string)
output += "<hr/>"
output += "{}</br>".format(soup.select_one("title").string)
output += "날짜: {}</br>".format(location.select_one("tmEf").string)
output += "지역: {}</br>".format(soup.select_one("province").string)
return output
# #5
# @app.route("/kma1")
# def kma1():
# # urlopen() 함수로 기상청의 전국 날씨를 읽습니다.
# target = request.urlopen("https://www.kma.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=159")
# # BeautifulSoup를 사용해 웹 페이지를 분석합니다.
# soup = BeautifulSoup(target, "html.parser")
# # location 태그를 찾습니다.
# output = ""
# # output +="<h3>{}</h3>".format(soup.select_one("title").string)
# # output += "날짜: {}<hr/>".format(soup.select_one("tmEf").string)
# for item in soup.select("item"):
# output += "<h3>{}</h3><hr/>".format(item.select_one("title").string)
# for location in soup.select("location"):
# # 내부의 city, wf, tmn, tmx 태그를 찾아 출력합니다.
# output += "<h3>{}</h3>".format(location.select_one("city").string)
# output += "날짜: {}</br>".format(location.select_one("tmEf").string)
# output += "날씨: {}</br>".format(location.select_one("wf").string)
# output += "최저/최고 기온: {}/{}".format(location.select_one("tmn").string, location.select_one("tmx").string)
# output += "<hr/>"
# return output
if __name__ == '__main__':
app.run(host='127.0.0.1', port='5000', debug=True)