본문 바로가기

크롤링

울산 코로나 환자 현황 크롤링

import requests
from bs4 import BeautifulSoup as bs

url = 'http://www.ulsan.go.kr/corona.jsp'

html = requests.get(url)

soup = bs(html.content, 'html.parser')

trs_patient = soup.find_all('tr', {'class':'patient'})

patients = []

for idx, tr_patient in enumerate(trs_patient):
    if idx != 0:
        tds_patient = tr_patient.findAll('td')
        col_patient = []
        for td_patient in tds_patient:
            col_patient.append(td_patient.text)
        patients.append(col_patient)

patients

import pandas as pd

df_corona = pd.DataFrame(patients, columns=['환자','거주지','추정감염경로','확진일','격리시설'])
df_corona.to_csv('corona_ulsan.csv')
df_corona.to_excel('corona_ulsan.xls')