[혼공학습단 13기] 6주차 과제

양승빈·2025년 2월 23일
0

혼공학습단 13기

목록 보기
8/9
post-thumbnail

그냥 대충 살겠어요.

나는 이 이야기를 무척 좋아한다...

그런 의미에서 이번 추가 과제도 좀 있다가 올리겠습니다...


6주차 과제

  • 기본 과제: p.431[직접 해보는 손코딩: Beatuiful Soup 스크레이핑 실행하기] 예제 실행 후 결과 화면 캡처하기
  • 추가 숙제: 혼공 용어 노트에 나만의 언어로 객체, 클래스, 인스턴스, 생성자, 메소드 정리하고 공유하기

기본 과제

  1. Beautiful Soup 스크레이핑
  • (Code)
# (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') 
  • (Result)

0개의 댓글

관련 채용 정보