Last active
August 29, 2015 14:03
-
-
Save allieus/65b95f301584cc932316 to your computer and use it in GitHub Desktop.
의안정보시스템 파싱 샘플 (추가 라이브러리 : BeautifulSoup, 설치 : pip install BeautifulSoup)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
import urllib | |
from BeautifulSoup import BeautifulSoup, NavigableString | |
class AssemblyParser(object): | |
def __init__(self, url): | |
self.url = url | |
html = urllib.urlopen(url).read() | |
self.soup = BeautifulSoup(html) | |
def get_string(self, parent): | |
l = [] | |
for tag in parent: | |
if isinstance(tag, NavigableString): | |
l.append(tag.string) | |
else: | |
l.extend(self.get_string(tag)) | |
return l | |
def clean(self, s): | |
s = s.replace(' ', ' ') | |
return ' '.join(s.split()) | |
def parse(self, text): | |
tag = self.soup.find('span', text=text) | |
table_tag = tag.parent.findNextSibling('table') | |
rows = [] | |
for tr_tag in table_tag.findAll('tr'): | |
row = [] | |
for td_tag in tr_tag: | |
if isinstance(td_tag, basestring): | |
# ignore | |
continue | |
col = ' '.join(self.clean(s) for s in self.get_string(td_tag) if s.strip()) | |
row.append(col) | |
rows.append(row) | |
return { | |
'header': rows[0], | |
'body': rows[1:], | |
} | |
def pprint(self, parsed): | |
for col in parsed['header']: | |
print col, '||', | |
for row in parsed['body']: | |
for col in row: | |
print col, '||', | |
if __name__ == '__main__': | |
url = "http://likms.assembly.go.kr/bill/jsp/BillDetail.jsp?bill_id=PRC_D1B3J1K0Z1W5K1J5T4F5Y2O7G4T9Q2&list_url=/bill/jsp/FinishBill.jsp%3F" | |
parser = AssemblyParser(url) | |
parsed = parser.parse(u'의안접수정보') | |
parser.pprint(parsed) | |
parsed = parser.parse(u'소관위 심사정보') | |
parser.pprint(parsed) | |
parsed = parser.parse(u'소관위 회의정보') | |
parser.pprint(parsed) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
의안번호 || 제안일자 || 제안자 || 문서 || 제안이유 및 주요내용 || 제안회기 || | |
1907282 || 2013-10-15 || 김성주의원 등 12인 || 의안원문 || || 제19대 (2012~2016) 제320 회 || | |
소관위원회 || 회부일 || 상정일 || 처리일 || 처리결과 || 문서 || | |
보건복지위원회 || 2013-10-16 || 2014-04-11 || 2014-04-23 || 폐기 || 검토보고서 심사보고서 || | |
회의명 || 회의일 || 회의결과 || 회의록 || | |
제323회 국회(임시회) 제2차 전체회의 || 2014-04-11 || 상정/상정/제안설명/제안설명/검토보고/검토보고/대체토론/대체토론/소위회부/소위회부 || || | |
제323회 국회(임시회) 제4차 법안심사소위 || 2014-04-17 || 상정/의결(본회의에 부의하지 아니하기로 의결) || || | |
제324회 국회(임시회) 제1차 전체회의 || 2014-04-23 || 상정/상정/소위심사보고/소위심사보고/의결(본회의에 부의하지 아니하기로 의결)/의결(본회의에 부의하지 아니하기로 의결) || || |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment