Skip to content

Instantly share code, notes, and snippets.

@esehara
Last active December 14, 2015 22:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esehara/5160234 to your computer and use it in GitHub Desktop.
Save esehara/5160234 to your computer and use it in GitHub Desktop.
郵便物検索結果の状態を取ってきて吐き出すスクリプト
config:
color: true
goods:
- URL: http://tracking.post.japanpost.jp/service/detail_search.do?searchKind=M004&reqCode=HOGEHOGE123
kind: 健康かつ快活になれる海外サプリメント
# -*- coding: utf-8 -*-
import yaml
import urllib
import sys
from termcolor import colored
from bs4 import BeautifulSoup
class InterStatus(object):
def __init__(self, conf):
print(u'Connecting....')
html = urllib.urlopen(conf['URL']).read()
print(u'Get URL soruce')
print(conf['URL'])
soup = BeautifulSoup(html)
try:
self.table_data = soup.find_all('table')[7].find_all('tr')
except IndexError:
print('!!!!! Invalid Input Error !!!!!')
print('Often, URL Error or Maintenance')
sys.exit(1)
self.kind = conf['kind']
self.is_pass_line = False
def colorize(self, string, color):
if self.is_color:
string = colored(string, color)
return string
def render(self, config):
self.is_color = (
'color' in config and config['color'])
p_str_line = self.colorize(
u'=' * 40, 'yellow')
p_str_name = self.colorize(
u'【' + self.kind + u'】\n', 'cyan')
print (
p_str_line + "\n" +
p_str_name +
p_str_line)
for tr in self.table_data:
line = self._generate_line(tr)
if line:
print line
print
def _generate_line(self, tr):
line = ""
first_line = True
for td in tr.find_all('td'):
if 'rowspan' in td.attrs and td['rowspan'] == "2":
self.is_pass_line = True
elif self.is_pass_line and not 'rowspan' in td:
self.is_pass_line = False
continue
td_text = td.text.rstrip().strip()
if td_text:
if first_line:
line = '%-15s|' % td_text
first_line = False
else:
line += u'{0: <20}'.format(td_text)
return line
def factory_InterStatus(configs):
return_interstatus = []
for conf in configs:
return_interstatus.append(
InterStatus(conf))
return return_interstatus
def parse_config(filepath):
conffile = open(filepath).read().decode('utf8')
conf = yaml.load(conffile)
return conf
def main():
yaml = parse_config('config.yaml')
config = yaml['config']
goods = yaml['goods']
status = factory_InterStatus(goods)
for state in status:
state.render(config)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment