p.342 [직접 해보는 손코딩:BeautifulSoup 스크레이핑 실행하기] 예제 실행 후 결과 화면 캡쳐하기
예제 코드
from flask import Flask
from urllib import request
from bs4 import BeautifulSoup
# 웹 서버 생성
app = Flask(__name__)
@app.route("/")
def hello():
# urlopen() : 기상청의 전국 날씨 읽기
target = request.urlopen("<https://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
실행
실행 결과
혼공 용어 노트에 나만의 언어로 객체, 클래스, 인스턴스, 생성자, 메소드 정리하고 공유하기
<aside> 💡 목차 클릭 시 관련 내용으로 이동합니다.
</aside>