Skip to content

Instantly share code, notes, and snippets.

@lanfon72
Last active August 29, 2015 14:04
Show Gist options
  • Save lanfon72/954b5d96ddf0e365f22f to your computer and use it in GitHub Desktop.
Save lanfon72/954b5d96ddf0e365f22f to your computer and use it in GitHub Desktop.
parse wanfangh live ER status board
#!/usr/bin/env python
#coding:UTF-8
import requests, re, json, os
from datetime import datetime
os.environ['TZ'] = 'ROC'
html = requests.get('http://www.wanfang.gov.tw/p11_information.aspx')
update_time = re.findall(u"日期:(.+?)</div",html.text)[0]
pending = re.findall(u"\'>(.+?)</div",html.text)[:-1]
# prase like ["<div class='p11_con'><div class='p11_icon05'>否", '1', '0', '16', '1']
pending[0] = pending[0][-1] #take "否" or "是"
values = [int(ele) for ele in pending[1:]]
keys = ['pending_doctor','pending_bed', 'pending_ward', 'pending_icu']
report = { key:value for value, key in zip(values, keys) }
report["hospital_sn"] = '1301200010'
report['full_reported'] = False if pending[0]==u'否' else True
report["update_time"] = datetime.strptime(update_time, '%Y/%m/%d %H:%M').strftime('%s')
#using .timestamp() if py3 else .strftime('%s')
print ( json.dumps(report, ensure_ascii=False) )
#[{"update_time": "1407225600", "pending_ICU": 2, "pending_bed": 0, "full_reported": false, "Hosptial_SN": "1301200010", "pending_doctor": 2, "pending_ward": 19}]
@thewayiam
Copy link

!/usr/bin/env python

coding:UTF-8

import requests, re, json
html = requests.get('http://www.wanfang.gov.tw/W402008web_new/epd_query.asp')
html.encoding='big5'

update_time = re.findall(u'日期:(.) ,系統',html.text)
pending = re.findall(u':(.
)',html.text)
keys = ['full_reported','pending_doctor','pending_bed', 'pending_ward', 'pending_ICU']

report = { key:value.replace(u' 人','').strip() for value, key in zip(pending, keys) }
update_time = update_time[0].replace(u':',':').replace('\u3000',' ')
report["Hosptial_SN"] = 1301200010
report["update_time"] = update_time
report = [report]

print ( json.dumps(report, ensure_ascii=False) )

@lanfon72
Copy link
Author

lanfon72 commented Aug 5, 2014

fix int value, strftime()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment