사용자:Utoleetest/covid-kr

< 사용자:Utoleetest
Utoleetest (토론 | 기여)님의 2021년 10월 12일 (화) 23:47 판 (소스 수정)

의존 확장기능

  • Seleniumr과 Chromedriver 기능

소스

import pywikibot
from selenium import webdriver
import re

driver= webdriver.Chrome()
driver.get('http://ncov.mohw.go.kr/')
date_s = driver.find_element_by_xpath('//*[@id="content"]/div[2]/div/div[1]/div[1]/h2/a/span[1]').text
date_month = re.match(r'^\((.*?)\.(.*?)\.', date_s).group(1)
date_date = re.match(r'^(.*?)\.(.*?)\.', date_s).group(2)
date = f"2021.{date_month.zfill(2)}.{date_date.zfill(2)} 00시; 질병관리청"
diagnosed = driver.find_element_by_xpath('//*[@id="content"]/div[2]/div/div[1]/div[1]/div[1]/div/ul/li[1]/span[2]').text
total = driver.find_element_by_xpath('//*[@id="content"]/div[2]/div/div[1]/div[1]/div[2]/ul/li[1]/span[1]').text[-7:]
add_s = driver.find_element_by_xpath('//*[@id="content"]/div[2]/div/div[1]/div[1]/div[2]/ul/li[1]/span[2]').text
add = re.match(r'전일대비 \(\+\s*(.*?)\)', add_s).group(1)

recover = driver.find_element_by_xpath('//*[@id="content"]/div[2]/div/div[1]/div[1]/div[2]/ul/li[2]/span[1]').text.replace('+','')
recover_add_s = driver.find_element_by_xpath('//*[@id="content"]/div[2]/div/div[1]/div[1]/div[2]/ul/li[2]/span[2]').text
recover_add = re.match(r'\(\+\s*(.*?)\)', recover_add_s).group(1)

active = driver.find_element_by_xpath('//*[@id="content"]/div[2]/div/div[1]/div[1]/div[2]/ul/li[3]/span[1]').text
active_add_s = driver.find_element_by_xpath('//*[@id="content"]/div[2]/div/div[1]/div[1]/div[2]/ul/li[3]/span[2]').text
# 전날보다 치료중 환자는 늘 수도, 줄 수도 있다. 따라서 -가 될 때도 고려해야 한다.
active_add = re.match(r'\(([+\-]\s*.*?)\)', active_add_s).group(1).replace(' ', '')

death = driver.find_element_by_xpath('//*[@id="content"]/div[2]/div/div[1]/div[1]/div[2]/ul/li[4]/span[1]').text
death_add_s = driver.find_element_by_xpath('//*[@id="content"]/div[2]/div/div[1]/div[1]/div[2]/ul/li[4]/span[2]').text
death_add = re.match(r'\(\+\s*(.*?)\)', death_add_s).group(1)

# 위키/목록 텍스트 추출
site = pywikibot.Site('ko', 'libre')
page = pywikibot.Page(site, "Template:COVID-19/확진자 개요")
txt = page.text

txt = re.sub(r'<!--date-->\(.*?\)', f'<!--date-->({date})', txt)
txt = re.sub(r'<!--total-->.*?명', f'<!--total-->{total}명', txt)
txt = re.sub(r'<!--add-->.*?명', f'<!--add-->+{add}명', txt)
txt = re.sub(r'<!--recover-->.*?명', f'<!--recover-->{recover}명', txt)
txt = re.sub(r'<!--recover_add-->.*?명', f'<!--recover_add-->+{recover_add}명', txt)
txt = re.sub(r'<!--active-->.*?명', f'<!--active-->{active}명', txt )
txt = re.sub(r'<!--active_calc-->\(.*?\)', f"<!--active_calc-->({{{{#expr:{active.replace(',','')}/{total.replace(',','')}*100 round 1}}}}%, 전일 대비 {active_add}명)", txt)
txt = re.sub(r'<!--death-->.*?명', f'<!--death-->{death}명', txt )
txt = re.sub(r'<!--death_calc-->\(.*?\)', f"<!--death_calc-->({{{{#expr:{death.replace(',','')}/{total.replace(',','')}*100 round 1}}}}%, 전일 대비 +{death_add}명)", txt)

page.text = txt
page.save('봇: COVID-19 확진자 정보 갱신')