Skip to content
This repository has been archived by the owner on Apr 25, 2022. It is now read-only.

Commit

Permalink
データが取得できなかった場合にスルーする処理を追加
Browse files Browse the repository at this point in the history
  • Loading branch information
sakurum committed Sep 15, 2020
1 parent 5679e95 commit 5b3f43e
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,24 +154,30 @@ def _update_nowinfectedperson(self):
print(type(e), e)
return
else:
soup = BeautifulSoup(response.content, features="html.parser")
try:
soup = BeautifulSoup(response.content, features="html.parser")

# 陽性患者数を取得
nip_str = soup.find("span", string="陽性患者数").next_sibling.next_sibling.find_all("span")[1].text
nip_str = nip_str.replace("名", "")
nip_num = int(nip_str.translate(str.maketrans({chr(0xFF01 + i): chr(0x21 + i) for i in range(94)})))
# 陽性患者数を取得
nip_str = soup.find("span", string="陽性患者数").next_sibling.next_sibling.find_all("span")[1].text
nip_str = nip_str.replace("名", "")
nip_num = int(nip_str.translate(str.maketrans({chr(0xFF01 + i): chr(0x21 + i) for i in range(94)})))

# その日付を取得
datestr = re.findall(r"入退院状況(令和[0-9 0-9]{1,2}年[0-9 0-9]{1,2}月[0-9 0-9]{1,2}日現在)", soup.find("div", class_="main-text").text)[0]
datestr = datestr.translate(str.maketrans({chr(0xFF01 + i): chr(0x21 + i) for i in range(94)}))
m = re.findall(r"\d+", datestr)
date = datetime.date(int(m[0])+2018, int(m[1]), int(m[2])).strftime("%Y-%m-%dT00:00:00.000+09:00")
# その日付を取得
datestr = re.findall(r"入退院状況(令和[0-9 0-9]{1,2}年[0-9 0-9]{1,2}月[0-9 0-9]{1,2}日現在)", soup.find("div", class_="main-text").text)[0]
datestr = datestr.translate(str.maketrans({chr(0xFF01 + i): chr(0x21 + i) for i in range(94)}))
m = re.findall(r"\d+", datestr)
date = datetime.date(int(m[0])+2018, int(m[1]), int(m[2])).strftime("%Y-%m-%dT00:00:00.000+09:00")

if self._data["nowinfectedperson"]["data"][-1]["日付"] == date:
return
if self._data["nowinfectedperson"]["data"][-1]["日付"] == date:
return

self._data["nowinfectedperson"]["data"].append({"日付": date, "小計": nip_num})
self._data["nowinfectedperson"]["date"] = self._get_lastupdate()
except Exception as e:
print(type(e), e)
return

else:
self._data["nowinfectedperson"]["data"].append({"日付": date, "小計": nip_num})
self._data["nowinfectedperson"]["date"] = self._get_lastupdate()


def _update_last_update(self):
Expand Down

0 comments on commit 5b3f43e

Please sign in to comment.