-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
48 lines (43 loc) · 1.94 KB
/
parser.py
File metadata and controls
48 lines (43 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#2018.03.27
import requests, re
from bs4 import BeautifulSoup
def get_diet(code, ymd, weekday):
schMmealScCode = code #int 1조식2중식3석식
schYmd = ymd #str 요청할 날짜 yyyy.mm.dd
if weekday == 5 or weekday == 6: #토요일,일요일 버림
element = " " #공백 반환
else:
num = weekday + 1 #int 요청할 날짜의 요일 0월1화2수3목4금5토6일 파싱한 데이터의 배열이 일요일부터 시작되므로 1을 더해줍니다.
URL = (
"http://stu.sen.go.kr/sts_sci_md01_001.do?"
"schulCode=B100000519"
"&schulCrseScCode=4"
"&schulKndScCode=04"
"&schMmealScCode=%d&schYmd=%s" % (schMmealScCode, schYmd)
)
#http://stu.AAA.go.kr/ 관할 교육청 주소 확인해주세요.
#schulCode= 학교고유코드
#schulCrseScCode= 1유치원2초등학교3중학교4고등학교
#schulKndScCode= 01유치원02초등학교03중학교04고등학교
#기존 get_html 함수부분을 옮겨왔습니다.
html = ""
resp = requests.get(URL)
if resp.status_code == 200 : #사이트가 정상적으로 응답할 경우
html = resp.text
soup = BeautifulSoup(html, 'html.parser')
element_data = soup.find_all("tr")
element_data = element_data[2].find_all('td')
try:
element = str(element_data[num])
#filter
element_filter = ['[', ']', '<td class="textC last">', '<td class="textC">', '</td>', '&', '(h)', '.']
for element_string in element_filter :
element = element.replace(element_string, '')
#줄 바꿈 처리
element = element.replace('<br/>', '\n')
#모든 공백 삭제
element = re.sub(r"\d", "", element)
#급식이 없을 경우
except:
element = " " # 공백 반환
return element