forked from a2ran/DSL-23-2-FingerFriend
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path공홈_공지.py
139 lines (100 loc) · 5.39 KB
/
공홈_공지.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import pandas as pd
import tqdm.notebook as tqdm
from bs4 import BeautifulSoup
if __name__ == "__main__":
driver = webdriver.Chrome('chromedriver.exe')
n = 0
cnt = 0
data_공지 = []
while True:
url_공지 = f"https://www.yonsei.ac.kr/sc/support/notice.jsp?mode=list&board_no=15&pager.offset={n}"
driver.get(url_공지)
cnt += 1
wait = WebDriverWait(driver, 3)
tbody = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#jwxe_main_content > div.jwxe_board > div > ul")))
rows = tbody.find_elements(By.TAG_NAME, "li")
if len(rows) <= 10:
break
elif (n % 100) == 0:
print(f"{cnt}번 페이지 크롤링중...")
if n == 0:
for row in rows:
title = row.find_element(By.CSS_SELECTOR, "a > strong").text.strip()
link = row.find_element(By.CSS_SELECTOR, "a").get_attribute("href").strip()
web = row.find_element(By.CSS_SELECTOR, "a > span").text.strip()
sub = row.find_element(By.CSS_SELECTOR, "a > span > span:nth-child(1)").text.strip()
date = row.find_element(By.CSS_SELECTOR, "a > span > span:nth-child(2)").text.strip()
data_공지.append([web, sub, title, link, date])
else:
for row in rows[6:]:
title = row.find_element(By.CSS_SELECTOR, "a > strong").text.strip()
link = row.find_element(By.CSS_SELECTOR, "a").get_attribute("href").strip()
web = row.find_element(By.CSS_SELECTOR, "a > span").text.strip()
sub = row.find_element(By.CSS_SELECTOR, "a > span > span:nth-child(1)").text.strip()
date = row.find_element(By.CSS_SELECTOR, "a > span > span:nth-child(2)").text.strip()
data_공지.append([web, sub, title, link, date])
n += 10
driver.quit()
df_공지 = pd.DataFrame(data_공지, columns=["학과", "서브", "제목", "링크", "등록일"])
df_공지['학과'] = df_공지['학과'].apply(lambda x: x.split(' ')[0])
## 외부 기관 공고
driver = webdriver.Chrome('chromedriver.exe')
n = 0
cnt = 0
data_외부 = []
while True:
url_외부 = f"https://www.yonsei.ac.kr/sc/support/etc_notice.jsp?mode=list&board_no=43&pager.offset={n}"
driver.get(url_외부)
cnt += 1
wait = WebDriverWait(driver, 3)
tbody = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#jwxe_main_content > div.jwxe_board > div > ul")))
rows = tbody.find_elements(By.TAG_NAME, "li")
if len(rows) <= 3:
break
elif (n % 100) == 0:
print(f"{cnt}번 페이지 크롤링중...")
for row in rows:
title = row.find_element(By.CSS_SELECTOR, "a > strong").text.strip()
link = row.find_element(By.CSS_SELECTOR, "a").get_attribute("href").strip()
web = row.find_element(By.CSS_SELECTOR, "a > span").text.strip()
sub = row.find_element(By.CSS_SELECTOR, "a > span > span:nth-child(1)").text.strip()
date = row.find_element(By.CSS_SELECTOR, "a > span > span:nth-child(2)").text.strip()
data_외부.append([web, sub, title, link, date])
n += 10
driver.quit()
df_외부 = pd.DataFrame(data_외부, columns=["학과", "서브", "제목", "링크", "등록일"])
df_외부['학과'] = df_외부['학과'].apply(lambda x: x.split(' ')[0])
## 코로나19 관련 공지사항
driver = webdriver.Chrome('chromedriver.exe')
n = 0
cnt = 0
data_코로나 = []
while True:
url_코로나 = f"https://www.yonsei.ac.kr/sc/support/corona_notice.jsp?mode=list&board_no=752&pager.offset={n}"
driver.get(url_코로나)
cnt += 1
wait = WebDriverWait(driver, 3)
tbody = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#jwxe_main_content > div.jwxe_board > div > ul")))
rows = tbody.find_elements(By.TAG_NAME, "li")
if len(rows) <= 3:
break
elif (n % 100) == 0:
print(f"{cnt}번 페이지 크롤링중...")
for row in rows:
title = row.find_element(By.CSS_SELECTOR, "a > strong").text.strip()
link = row.find_element(By.CSS_SELECTOR, "a").get_attribute("href").strip()
web = row.find_element(By.CSS_SELECTOR, "a > span").text.strip()
sub = row.find_element(By.CSS_SELECTOR, "a > span > span:nth-child(1)").text.strip()
date = row.find_element(By.CSS_SELECTOR, "a > span > span:nth-child(2)").text.strip()
data_코로나.append([web, sub, title, link, date])
n += 10
driver.quit()
df_코로나 = pd.DataFrame(data_코로나, columns=["학과", "서브", "제목", "링크", "등록일"])
df_코로나['학과'] = df_코로나['학과'].apply(lambda x: x.split(' ')[0])
## concat 후 저장
df_공홈 = pd.concat([df_공지, df_외부, df_코로나])
df_공홈.to_csv('공홈.csv', index = False)