-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCafeteriaCrawler.py
75 lines (57 loc) · 2.78 KB
/
CafeteriaCrawler.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
koreanFoodTag = 'body > div > div > table > tr:nth-child({}) > td:nth-child(2)'
specialFoodTag = 'body > div > div > table > tr:nth-child({}) > td:nth-child(3)'
onedishFoodTag = 'body > div > div > table > tr:nth-child({}) > td:nth-child(4)'
westernFoodTag = 'body > div > div > table > tr:nth-child({}) > td:nth-child(5)'
facultyFoodTag = 'body > div > div > table > tr:nth-child({}) > td:nth-child(6)'
foodType = {0:koreanFoodTag, 1:specialFoodTag, 2:onedishFoodTag, 3:westernFoodTag, 4:facultyFoodTag}
foodTitle = {0:':rice: *한식* :rice:', 1:':curry: *일품식* :curry:', 2:':shallow_pan_of_food: *전골* :shallow_pan_of_food:', 3:':hamburger: *양식* :hamburger:', 4:':bento: *능수관* :bento:'}
class CafeteriaCrawler :
def __init__(self, soup):
self.soup = soup
self.todayDiet = []
self.Message = ''
def crawling(self) :
koreafood = []
specialfood = []
onedishfood = []
westernfood = []
facultyfood = []
for kind in range(len(foodType)) :
self.todayDiet = [koreafood,specialfood,onedishfood,westernfood,facultyfood]
for time in range(4,6+1) :
selector = self.soup.select(foodType[kind].format(time))
for instance in selector:
diet = instance.text
diet = diet.replace('\n','\r').replace('\r','\t').split('\t')
for i in diet :
if i != '' :
self.todayDiet[kind].append(i)
if self.todayDiet[kind][0] != '\xa0':
self.todayDiet[kind].append(i)
return self
def formatData(self) :
time = [' ==== 아침 메뉴 ====',' ==== 점심 메뉴 ====',' ==== 저녁 메뉴 ====']
for i in range(len(self.todayDiet)) :
self.Message += foodTitle[i]
self.Message += '\n' + ' ̄'*16
if self.todayDiet[i][0] != '\xa0' :
self.Message += '\n' + time[0]
nCnt, lunch, dinner = 0, False, False
for j in self.todayDiet[i] :
if j == '\xa0' :
nCnt += 1
else :
if nCnt == 1 and j != '' and lunch == False :
self.Message += '\n' + time[1]
lunch = True
elif nCnt == 2 and j != '' and dinner == False:
self.Message += '\n' + time[2]
dinner = True
if len(j) > 2:
self.Message += '\n'+' '*5 + j
self.Message += '\n' + ' ̄'*16 + '\n\n'
return self
def getAnswer(self) :
if self.Message == '' :
self.formatData()
return self.Message