Skip to content

Instantly share code, notes, and snippets.

@lanfon72
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lanfon72/b5e0e2299ae0180c16c7 to your computer and use it in GitHub Desktop.
Save lanfon72/b5e0e2299ae0180c16c7 to your computer and use it in GitHub Desktop.
parse pohai 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.pohai.org.tw/pohai/bedqty_er/bedqty_er.php')
html.encoding='utf8'
update_time = re.findall(u'日期:(.*)</td>',html.text)
pending = re.findall(u'center">(.+?)人</td>',html.text)
# prase like ['否</td>\t\t\t\t\t\t<td align="center">0 ', '0 ', '8 ', '0 ']
# [ report+doctor, bed, ward, ICU ]
p = pending[0].split('</td>\t\t\t\t\t\t<td align="center">')
# parse [report+doctor] -> ['否', '0 ']
full_reported = False if p[0]==u'否' else True
pending_doctor = int( p[1] )
pending_bed = int( pending[1] )
pending_ward = int( pending[2] )
pending_ICU = int( pending[3] )
update_time = datetime.strptime(update_time[0].strip(), '%Y/%m/%d %H:%M:%S').strftime('%s')
# using .timestamp() if python3 else .strftime('%s')
report = { "hospital_sn":'1434020015', "update_time":update_time, "full_reported":full_reported, "pending_doctor":pending_doctor, "pending_bed":pending_bed, "pending_ward":pending_ward, "pending_icu":pending_ICU };
print ( json.dumps(report, ensure_ascii=False) )
#[{"full_reported": false, "pending_ward": 3, "update_time": 1407218898.0, "pending_doctor": 0, "pending_bed": 0, "Hosptial_SN": 1434020015, "pending_ICU": 0}]
@thewayiam
Copy link

I need some adjust on line 17, 22 to make it work on python 2.7

!/usr/bin/env python

coding:UTF-8

import requests, re, json
from datetime import datetime
html = requests.get('http://www.pohai.org.tw/pohai/bedqty_er/bedqty_er.php')
html.encoding='utf8'

update_time = re.findall(u'日期:(.*)',html.text)
pending = re.findall(u'center">(.+?)人',html.text)
p = pending[0].split('\t\t\t\t\t\t')

full_reported = False if p[0]==u'否' else True
pending_doctor = int( p[1] )
pending_bed = int( pending[1] )
pending_ward = int( pending[2] )
pending_ICU = int( pending[3] )
update_time = datetime.strptime(update_time[0].strip(), '%Y/%m/%d %H:%M:%S').strftime('%s')

report = [{ "Hosptial_SN":'1434020015', "update_time":update_time, "full_reported":full_reported, "pending_doctor":pending_doctor, "pending_bed":pending_bed, "pending_ward":pending_ward, "pending_ICU":pending_ICU }];
print ( json.dumps(report, ensure_ascii=False) )

@lanfon72
Copy link
Author

lanfon72 commented Aug 5, 2014

sorry 忘了+ "u" QQ

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