728x90

# 모듈을 읽어 들입니다.
from flask import Flask
from urllib import request
from bs4 import BeautifulSoup
# 웹 서버를 생성합니다.
app = Flask(__name__)
@app.route("/")
def hello():
# urlopen() 함수로 기상청의 전국 날씨를 읽습니다.
target = request.urlopen("http://www.kma.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=108")
# BeautifulSoup를 사용해 웹 페이지를 분석합니다.
soup = BeautifulSoup(target, "html.parser")
# location 태그를 찾습니다.
output = ""
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/>"
return output

'Language > python' 카테고리의 다른 글
파이썬,DB 연결 (SQLAlchemy, mysql-connector-python) (0) | 2021.02.25 |
---|---|
Strip(), sep(),zip() (0) | 2021.02.21 |
여러가지 표기법 (0) | 2021.02.09 |
본격적인 API 개발-미니터 만들기. (1) 회원가입, 트윗 글 엔드포인트 (0) | 2021.02.09 |
About HTTP (feat. 쿠키, 세션) (0) | 2021.01.29 |