본문 바로가기
개발 업무(코딩)-개발자였을때 썼던..

파이썬(python) 크롤링(crawling) 간단 예제(코로나 확진자수 확인)

by 주용사 2023. 1. 8.
728x90
from urllib.request import urlopen
from bs4 import BeautifulSoup

html = urlopen("http://ncov.mohw.go.kr/bdBoardList_Real.do?brdId=1&brdGubun=11&ncvContSeq=&contSeq=&board_id=&gubun=")
bsObject = BeautifulSoup(html, "html.parser")

#print(bsObject)
table = bsObject.find('table', {'class':'num'})
trs = table.find_all('tr')

for idx, tr in enumerate(trs):
        if idx >= 0:
                tds = tr.find_all('td')
                ths = tr.find_all('th')
                print(tds[0].text.strip(), ths[0].text.strip())

python test.py

https://yuda.dev/260

만약 실행이 안되면

pip install beautifulsoup4를 사용해서 설치하자

728x90