1. 가상환경 설정 

- VScode 터미널에서 가상환경 설정 

※ 직접 설치하고 하니에러가 나서 제거 후 가상환경 구성 

# 가상 환경 생성
# venv 폴더가 생성 된다.
python -m venv venv

# 가상환경 활성화
.\venv\Scripts\activate


2. install 

pip install beautifulsoup4
pip install flask

 

3. path 설정

-  윈도우 패스 설정 

 

4. 코드 작성 

from flask import Flask
from urllib import request
from bs4 import BeautifulSoup

app = Flask(__name__)

@app.route("/")

def hello():
    target = request.urlopen("http://www.kma.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=108")

    soup = BeautifulSoup(target, "html.parser")

    output = ""

    for location in soup.select("location"):
        output += "<h3>{}</h3>".format(location.select_one("city").string)
        output += "날씨{}</br>".format(location.select_one("wf").string)
        output += "최저/최고 기온{}/{}</h3>".format(location.select_one("tmn").string, location.select_one("tmx").string)
        output += "</hr>"
    return output

 

5. 실행 

 

$env:FLASK_APP="beautiful_flask.py"
flask run

 

6. 확인 

-   크롬에서 확인

http://127.0.0.1:5000

 

 

+ Recent posts