728x90
강동구청 조직도 크롤링 소스코드이다.
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
|
import requests
from bs4 import BeautifulSoup
import pandas as pd
import re
def crawl_orgchart():
url = "https://www.gangdong.go.kr/web/newportal/empSearch/list"
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"}
response = requests.get(url, headers=headers)
if response.status_code != 200:
print("Failed to retrieve page")
return None
soup = BeautifulSoup(response.text, "html.parser")
data = []
departments = soup.select("h4.title-green")
tables = soup.select("table")
for dept, table in zip(departments, tables):
department_name = dept.text.strip()
# 괄호가 포함되어 있으면 괄호 이전 글자만 추출
if "(" in department_name:
department_name = re.split("\\(", department_name)[0].strip()
else:
# 'FAX' 이전까지의 글자만 추출
department_name = re.split("FAX", department_name, flags=re.IGNORECASE)[0].strip()
table_rows = table.select("tbody tr")
for row in table_rows:
columns = row.find_all("td")
if len(columns) >= 3:
position = columns[0].text.strip()
phone = columns[1].text.strip()
duty = columns[2].text.strip()
# 전화번호가 없는 행은 제외
if not phone:
continue
# 전화번호가 2줄 이상이면 한 칸 공백으로 띄우고 한 줄로 변환
phone = " ".join(phone.splitlines()).strip()
# 여러 줄인 경우 한 칸 공백으로 띄우고 한 줄로 변환
duty = " ".join(duty.splitlines()).strip()
# 담당업무가 1줄이고 첫 글자가 '-'이면 'o'로 변경
if duty.startswith("-") and "\n" not in duty:
duty = "o" + duty[1:]
data.append({
"부서명": department_name,
"직위": position,
"전화번호": phone,
"담당업무": duty
})
df = pd.DataFrame(data)
return df
if __name__ == "__main__":
df = crawl_orgchart()
if df is not None:
print(df)
df.to_csv("강동구청_조직도.csv", index=False, encoding="utf-8-sig")
|
728x90
'Web 크롤링 > Python Crawling' 카테고리의 다른 글
종로구청 조직도 크롤링 (0) | 2025.03.01 |
---|---|
[Python] 종로구청 동주민센터 크롤링 (0) | 2025.02.18 |
정부(행정안전부) 주소 검증 (0) | 2024.04.20 |
파이썬 selenium CentOS 7 환경설정 및 juso.go.kr 자료 파싱처리 (0) | 2024.03.25 |
파이썬 selenium 드라이버 설치 없이 사용하자. (0) | 2024.03.24 |